【问题标题】:ASP.NET 4.0 URL Routing HTTP Error 404.0 - Not FoundASP.NET 4.0 URL 路由 HTTP 错误 404.0 - 未找到
【发布时间】:2011-04-01 04:44:14
【问题描述】:

我已经使用以下路由在 ASP.NET 4.0 中实现了 URL 路由。

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

这给了我像

这样的网址
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,那么它会显示页面。

有人遇到同样的问题吗?

如何设置网址 http://www.mysie.com/news/1/this-is-test-news 在 windows server 2008 上工作?

【问题讨论】:

  • 该路由的文件是否与aspx页面在同一目录下?
  • 没有。我的 aspx 文件位于根文件夹

标签: routing asp.net-4.0


【解决方案1】:

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

  1. 确保您已安装 HTTP 重定向功能 可以这样做 -> 控制面板 -> 程序 -> 关闭 windows 功能 -> 万维网服务 -> 通用 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 应用程序池,因为路由不适用于 Asp.net 4.0 经典应用程序池。

希望这会有所帮助。

【讨论】:

  • 这真的应该说UrlRoutingHandler 2.0.0.0吗?为什么不是 4.0?
  • 这对我有用,但我不需要添加 UrlRoutingHandler 节点。
  • 我遇到了类似的情况,但是这个 asnwer 没有用,你能看看:stackoverflow.com/questions/34080141/…
【解决方案2】:

我已阅读您的所有食谱,但我的网站(ASP.NET 4.0 + VS2010 + Cassini)仍然没有正确路由

我网站的虚拟路径是“CompanyName.ApplicationName.Web”。我将此虚拟更改为“MyApplicationName”,瞧!

更改 Cassini 的虚拟路径配置:

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

【讨论】:

  • 很高兴我找到了这个答案!我正在使用带有句点“。”的虚拟路径。在里面。将我的虚拟路径从“mysite.com”更改为“mysite”后,我的所有自定义路由都正常工作。
【解决方案3】:

我的解决方案,在尝试了一切之后:

错误的部署,一个旧的 PrecompiledApp.config 挂在我的部署位置,导致一切都无法正常工作。

我的最终设置有效:

  • IIS 7.5,Win2k8r2 x64,
  • 集成模式应用程序池
  • 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;
        }
    }
    

【讨论】:

    猜你喜欢
    • 2016-03-08
    • 1970-01-01
    • 2011-08-06
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多