自定义路由在ASP.NET中不起作用

时间:2013-03-08 20:21:33

标签: c# webforms asp.net-routing

我一直在尝试关注此http://msdn.microsoft.com/en-us/library/cc668202(v=vs.90).aspx

我已创建将此添加到我的web.config:

<modules>
      <remove name="ScriptModule"/>
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <remove name="UrlRoutingModule" />
      <add name="UrlRoutingModule"
           type="System.Web.Routing.UrlRoutingModule, 
               System.Web.Routing, 
               Version=3.5.0.0, 
               Culture=neutral, 
               PublicKeyToken=31BF3856AD364E35"/>
    </modules>

并且,我已将此添加到我的Global.asax:

protected void Application_Start(Object sender, EventArgs e)
        {
            SplendidInit.InitApp();
            RegisterRoutes(RouteTable.Routes);
        }

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.Add("ShortUrl_Attachment_NoLogin", new Route
            (
               "a",
               new CustomRouteHandler("~/FACTS/Attachments/Attachment_NoLogin.aspx")
            ));
        }

        public class CustomRouteHandler : IRouteHandler
        {
            public CustomRouteHandler(string virtualPath)
            {
                this.VirtualPath = virtualPath;
            }

            public string VirtualPath { get; private set; }

            public IHttpHandler GetHttpHandler(RequestContext
                  requestContext)
            {
                var page = BuildManager.CreateInstanceFromVirtualPath
                     (VirtualPath, typeof(Page)) as IHttpHandler;
                return page;
            }
        }

当我导航到/FACTS/Attachments/Attachment_NoLogin.aspx时,它有效;但是,当我尝试导航到我的自定义路线/a时,它会说“ HTTP错误404.0 - 未找到。”

如何使自定义路线正常工作?

感谢您的帮助。

修改

我在IIS 7.5上使用.NET 3.5,如果这有所不同(我认为它确实如此)。

1 个答案:

答案 0 :(得分:0)

我使用的解决方案是下载并安装IIS Rewrite模块。这非常简单,非常酷:)

http://www.iis.net/downloads/microsoft/url-rewrite