bobek
bobek

Reputation: 8020

C# how to determine if the Request is not coming from an asset url, like image or css for example

I would like to do something like this:

if(Request == "mainPath")
{
//code here
}

So for example if the Request is coming from http://www.mydomain.com/tax/tax1 it would return true, but if Request is coming from http://www.mydomian.com/tax/tax1/image5.jpg or template.css it will not. What would be the best way to do it? I guess I could compare the url that's in the browser with the full path of the request but I am not sure if it's the best idea. Also, if it has any value to the question, the code exists in Application_BeginRequest() in Global.asax.cs - MVC3 project.

Thanks a lot!

Upvotes: 1

Views: 1101

Answers (2)

Syed Bashar
Syed Bashar

Reputation: 116

You can check the ContentType of Request. For resources like image you will get image/jpeg. you can determine which type of requesting is coming.

The correct Content-Type values should be

text/css -> for .css files

image/gif -> for .gif files

image/jpeg -> for .jpg/.jpeg files

Upvotes: 0

SLaks
SLaks

Reputation: 888047

It sounds like you're asking how to check the extension of the requested URL.
Check Path.GetExtension(Request.Url.LocalPath).

You might also want to check File.Exists(Server.MapPath(Request.AppRelativeCurrentExecutionFilePath)).

Upvotes: 2

Related Questions