ASP.NET MVC 路由 - 向路由添加 .html 扩展名

发布于 2025-01-07 05:06:18 字数 710 浏览 0 评论 0原文

我对 MVC 和路由非常陌生,我被要求修改一个应用程序以使用不同的 url。 由于我没有经验,这项任务对我来说有点困难。

好的,让我们谈谈一些代码:

routes.MapRoute(
"CategoryBySeName", // Route name
"products/{SeName}", // URL with parameters
new { controller = "Catalog", action = "CategoryBySeName" }
);

这按预期工作,但是客户端想要在路径末尾添加“.html”,所以我将:更改

"products/{SeName}", // URL with parameters

为:

"products/{SeName}.html", // URL with parameters

失败(IIS 404页面 - MapRequestHandler) 看起来 iis 正在尝试加载具有该名称的物理文件,而不是将其传递给应用程序。

类似: ASP.NET MVC 路由从 html 页面启动(未回答,不重复)

i am pretty new to MVC and Routing and i was asked to modify an app to use diffrent url's.
a task that is a bit over me since i have no experience.

ok, lets talk a bit of code:

routes.MapRoute(
"CategoryBySeName", // Route name
"products/{SeName}", // URL with parameters
new { controller = "Catalog", action = "CategoryBySeName" }
);

this works as expected, but then the client wanted ".html" at the end of paths, so i changed:

"products/{SeName}", // URL with parameters

to:

"products/{SeName}.html", // URL with parameters

which fails ( IIS 404 page - MapRequestHandler)
it seems like iis is trying to load a physical file with that name instead of passing it to the application.

Similar: ASP.NET MVC Routing to start at html page (not answered, Not duplicate)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

謌踐踏愛綪 2025-01-14 05:06:18

您必须强制所有请求通过 ASP.NET 管道,并且可以通过仅将这一行添加到应用程序的 web.config 来实现此目的:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

You have to force all request through the ASP.NET pipeline, and you can do that by adding only this single line to the web.config of your application:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
栩栩如生 2025-01-14 05:06:18

您猜测 IIS 处理程序可能会在 MVC 之前获取请求,这可能是正确的。

假设 IIS 7:
http://technet.microsoft.com/en- us/library/cc770990(v=ws.10).aspx

您需要在 IIS 中编辑 .html 处理程序才能使用 ASP.NET。

您可以在 II6 的映射部分的应用程序配置的主目录选项卡下的网站属性中找到它。

大致如下(版本可能不同):
C:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll 是处理 .html 文件所需的。

You're guess that an IIS handler is probably grabbing the request prior to MVC is likely correct.

Assuming IIS 7:
http://technet.microsoft.com/en-us/library/cc770990(v=ws.10).aspx

You need to edit the .html handler in IIS to use ASP.NET.

You can find it in the website properties under the home directory tab in app configuration in the mappings section in II6.

Something along the lines of (version may be different):
C:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll is what you need to handle the .html files.

当梦初醒 2025-01-14 05:06:18

将应用程序池从经典更改为集成解决了该问题。
谢谢你的帮助。

Changing the Application Pool from Classic to Integrated fixed the issue.
thank you guyz for your help.

别理我 2025-01-14 05:06:18

只需将此部分添加到 Web.config 中,所有对route/{*pathInfo} 的请求都将由指定的处理程序处理,即使 pathInfo 中有点也是如此。 (取自 ServiceStack MVC Host Web.config 示例和此答案 https://stackoverflow.com/a/12151501/801189

  <location path="route">
    <system.web>
      <httpHandlers>
        <add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
      </httpHandlers>
    </system.web>
    <!-- Required for IIS 7.0 -->
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
    </system.webServer>
  </location>

Just add this section to Web.config, and all requests to the route/{*pathInfo} will be handled by the specified handler, even when there are dots in pathInfo. (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189)

  <location path="route">
    <system.web>
      <httpHandlers>
        <add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
      </httpHandlers>
    </system.web>
    <!-- Required for IIS 7.0 -->
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
    </system.webServer>
  </location>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文