ASP.NET Request Extension Type

I am working on a large web application that I recently pushed back from a .aspx pages project.

To avoid page error, I added these objects to the xml, which totaled about 300+. I wrote a http module that checks the request url in the xml objects and if found, my module will redirect the request to the appropriate new pages.

Everything works great, but my collection is iterating for all requests, I mean for each .jpg, .css, .js, .ico, .pdf, etc.

Is there any object or property in .net that can indicate the type of request requested by the user as HttpContext.request.type

. Therefore, I can avoid checking the request for all unwanted file types.

+2


a source to share


3 answers


You can use a property HttpContext.Current.Request.FilePath

that gets the virtual path of the current request.

For example, for a URL, the http://www.contoso.com/virdir/page.html/tail

value FilePath

is /virdir/page.html

.



The second step is to get the extension itself. For example, you can do this with the method System.IO.Path.GetExtension

. For the path, /virdir/page.html

it will return the extension .html

.

+1


a source


Instead, you can only catch 404 errors to avoid intercepting every page request. Add Application_Error method to your global.asax as shown below. This will also allow you to redirect to a special error page if the page is not the one you need to redirect according to your XML file.



protected void Application_Error(object sender, EventArgs e)
{
    Log.Error("*** Application_Error ***");

    Exception baseException = Server.GetLastError().GetBaseException();

    HttpException httpException = baseException as HttpException;
    if (httpException != null)
    {
        int httpCode = httpException.GetHttpCode();
        Log.ErrorFormat("HTTPEXCEPTION: {0} : {1}", httpCode, HttpContext.Current.Request.RawUrl);
        if (httpCode == 404)
        {
                 ...

      

+1


a source


Another approach is to use ASP.NET Routing (from .NET 3.5) to create routes that map each of the old pages to the new page handler. ASP.NET routing makes it easy to have multiple Urls for a single ASPX page, and you can actually hide the .ASPX completely from the end user and instead have SEO friendly search queries for all of your pages. If you are mapping multiple URLs on the same page, you want to put the canonical URL on the page.

As an alternative

If you want redirects, you can register routes with a simple redirect routing handler like this: -

   routes.Add(new Route("sample.aspx", new RedirectRouteHandler("/home/newsample.aspx")));

      

And RedirectRouteHandler might look something like this: -

    /// <summary>
    /// Redirect Route Handler
    /// </summary>
    public class RedirectRouteHandler : IRouteHandler
    {

    private string newUrl;

    public RedirectRouteHandler(string newUrl)
    {
        this.newUrl = newUrl;
    }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new RedirectHandler(newUrl);
    }
}

/// <summary>
/// <para>Redirecting MVC handler</para>
/// </summary>
public class RedirectHandler : IHttpHandler
{
    private string newUrl;

    public RedirectHandler(string newUrl)
    {
        this.newUrl = newUrl;
    }

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext httpContext)
    {
        httpContext.Response.Status = "301 Moved Permanently";
        httpContext.Response.StatusCode = 301;
        httpContext.Response.AppendHeader("Location", newUrl);
        return;
    }
}

      

+1


a source







All Articles