2010-08-19 57 views
12

我使用以下路由在ASP.NET 4.0中实现了URL路由。ASP.NET 4.0 URL路由HTTP错误404.0 - 未找到

routes.MapPageRoute(
    "NewsDetails",    // Route name 
    "news/{i}/{*n}", // Route URL 
    "~/newsdetails.aspx"  // Web page to handle route 
    ); 

这给了我的url像

http://www.mysie.com/news/1/this-is-test-news 

,这是工作在我的本地主机的罚款。

但是当我上传到服务器上它给...

Server Error 

404 - File or directory not found. 
The resource you are looking for might have been removed, had its name changed, 
or is temporarily unavailable. 

如果我尝试http://www.mysie.com/news/1/this-is-test-news.aspx然后显示页面。

有没有人有同样的问题?

如何设置URL http://www.mysie.com/news/1/this-is-test-news在Windows Server 2008上工作?

+0

路径与aspx页面在同一目录中的文件是? – XstreamINsanity 2010-08-19 15:19:11

+0

没有。我的aspx文件在根文件夹 – 2010-08-19 15:26:43

回答

33

要启用默认ASP.Net 4.0路由与IIS 7.5:

  1. 确保您已经安装了HTTP重定向功能 这是可以做到 - >控制面板 - >程序 - >关闭窗口功能 - >万维网服务 - >常见HTTP功能 - > HTTP重定向
  2. 与下面的代码修改您web.config

 

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true">  
     <remove name="UrlRoutingModule"/> 
     <add name="UrlRoutingModule" 
      type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
    <handlers> 
     <add name="UrlRoutingHandler" 
      preCondition="integratedMode" 
      verb="*" 
      path="UrlRouting.axd" 
      type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
    </handlers> 
</system.webServer> 

3.您global.asax文件创建路线

注意:您必须应用程序池设置为Asp.net 4.0应用程序池,如路由不与天冬氨酸工作。 net 4.0 Classic应用程序池。

希望这会有所帮助。

+0

这真的应该说'UrlRoutingHandler 2.0.0.0'?为什么不是4.0? – Maslow 2011-06-30 17:37:59

+0

这对我有用,但我不需要添加UrlRoutingHandler节点。 – Mike 2012-09-18 17:09:16

+0

真棒..工作像魅力:) – 2013-12-04 09:41:24

1

我读过你所有的食谱,但我的网站(ASP.NET 4.0 + VS2010 +卡西尼)仍然没有正确路由

虚拟路径为我的网站是“CompanyName.ApplicationName.Web”。我把这个虚拟变成了“MyApplicationName”,瞧!

更改卡西尼虚拟路径配置:

  • 卡西尼虚拟路径 - >按Ctrl + W,P或;
  • 右键单击网站和“属性窗口”。
+0

很高兴我找到了答案!我正在使用有一段时间的虚拟路径。“在里面。将我的虚拟路径从“mysite.com”更改为“mysite”后,我所有的自定义路由都起作用了。 – 2013-03-11 18:59:35

1

我的解决办法,乱投医后:

坏部署,旧PrecompiledApp.config被挂在我的部署位置,使一切不工作。

我认为工作最终设置:

  • IIS 7.5,Win2k8r2 64,在web.config
  • 集成模式的应用程序池
  • 没有什么变化 - 这意味着路由无特殊处理。这里是我对许多其他帖子的参考部分的快照。我使用FluorineFX,所以我有一个处理程序添加,但我并不需要任何其他:

    <system.web> 
        <compilation debug="true" targetFramework="4.0" /> 
        <authentication mode="None"/> 
    
        <pages validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/> 
        <httpRuntime requestPathInvalidCharacters=""/> 
    
        <httpModules> 
        <add name="FluorineGateway" type="FluorineFx.FluorineGateway, FluorineFx"/> 
        </httpModules> 
    </system.web> 
        <system.webServer> 
        <!-- Modules for IIS 7.0 Integrated mode --> 
        <modules> 
         <add name="FluorineGateway" type="FluorineFx.FluorineGateway, FluorineFx" /> 
        </modules> 
    
        <!-- Disable detection of IIS 6.0/Classic mode ASP.NET configuration --> 
        <validation validateIntegratedModeConfiguration="false" /> 
        </system.webServer> 
    
  • Global.ashx:(仅适用于任何音符的方法)

    void Application_Start(object sender, EventArgs e) { 
        // Register routes... 
        System.Web.Routing.Route echoRoute = new System.Web.Routing.Route(
          "{*message}", 
         //the default value for the message 
          new System.Web.Routing.RouteValueDictionary() { { "message", "" } }, 
         //any regular expression restrictions (i.e. @"[^\d].{4,}" means "does not start with number, at least 4 chars 
          new System.Web.Routing.RouteValueDictionary() { { "message", @"[^\d].{4,}" } }, 
          new TestRoute.Handlers.PassthroughRouteHandler() 
         ); 
    
        System.Web.Routing.RouteTable.Routes.Add(echoRoute); 
    } 
    
  • PassthroughRouteHandler的.cs - 这实现从http://andrew.arace.info/stackoverflowhttp://andrew.arace.info/#stackoverflow自动转换这将随后由Default.aspx的处理:

    public class PassthroughRouteHandler : IRouteHandler { 
    
        public IHttpHandler GetHttpHandler(RequestContext requestContext) { 
         HttpContext.Current.Items["IncomingMessage"] = requestContext.RouteData.Values["message"]; 
         requestContext.HttpContext.Response.Redirect("#" + HttpContext.Current.Items["IncomingMessage"], true); 
         return null; 
        } 
    }