简体   繁体   中英

Determine if path is cross domain

How can I determine a given virtual path is within the domain during Application_Start?

// Cross domain virtual path
"//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"

// In app domain virtual path
"~/scripts/jquery/1.11.0/jquery.min.js" is not.

Edit :

Can I maybe just use

VirtualPathUtility.IsRelative()

Not sure what you are trying to do, but you could test and see if the file is actually there:

if (File.Exists(Server.MapPath(path)))
{
    //file is on the server
}

UPDATE: In response to your question if you can use VirtualPathUtility.IsRelative() : yes you could. Per MSDN :

Returns a Boolean value indicating whether the specified virtual path is relative to the application.

The difference from what I showed above is File.Exists will return false if the file doesn't actually exist. VirtualPathUtility.IsRelative() will return true for a path that is relative, whether a file is there or not.

What you use depends on what you are trying to accomplish, which isn't quite clear.

You can not.

Because there is no path during Application_Start

Application_Start is triggered when the application starts before and page is processed and especially before any HTML is sent.

As incidentally the path you are talking about is and has to be part of the HTML output.... which is in a View.... this is very very very very very very early late in the processing.

For that you ffirst need to identify the view which needs running a control which needs analyzoing and routing a request which needs having a started app - and that last part, is where you want to know the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM