2010-04-20 122 views
2

我正在研究一个大型Web应用程序,我最近从项目中搁置了大量的.aspx页面ASP.NET请求扩展类型

为了避免页面未找到错误,我在xml中添加了这些实体,其数量大约在300+以上。我编写了一个http模块,用于检查xml实体中的请求url,如果发现它们,我的模块将把请求重定向到相应的新页面。

一切都很正常,但我的收藏是越来越重复的所有请求,我的意思是每一个.JPG,的.css,.js文件,.ICO,.PDF等

有什么对象或属性,可以告诉用户请求的请求的类型,如HttpContext.request.type。这样我可以避免检查所有不需要的文件类型的请求。

+0

您运行的是哪个版本的IIS? – David 2010-04-20 21:52:46

回答

1

您可以使用HttpContext.Current.Request.FilePath属性获取当前请求的虚拟路径。

例如,对于URL http://www.contoso.com/virdir/page.html/tailFilePath的值为/virdir/page.html

第二步是获取扩展本身。例如,您可以使用System.IO.Path.GetExtension方法来执行此操作。对于/virdir/page.html路径,它将返回.html扩展名。

1

您可以改为捕获404错误以避免拦截每个页面请求。将Application_Error方法添加到您的global.asax中,如下所示。如果页面不是您需要根据您的XML文件重定向的页面,这将允许您也重定向到一个特殊的错误页面。

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

另一种方法是使用ASP.NET路由(从.NET 3.5开始)创建将每个旧页面映射到新页面处理程序的路由。 ASP.NET路由可以让一个ASPX页面拥有多个Url,并且实际上可以完全隐藏最终用户的.ASPX,并且可以为您的所有页面提供搜索引擎友好的Url。如果您将多个网址映射到一个网页上,则您需要在网页上放置规范网址标记。

或者

如果你想重定向你可以用一个简单的重定向路由处理这样的注册路线: -

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

而且RedirectRouteHandler可能是这个样子: -

/// <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; 
    } 
} 
+0

我已经有了这些路线。我需要这些路线的重定向。 – Krishna 2010-04-21 02:07:58

+0

如果您不想将它们映射到同一页面(正如我所建议的那样),您可以随时注册一个重定向路由处理程序,如我的编辑中所示。 – 2010-04-21 05:15:09