我有一个项目,要求我的url在路径上有圆点。例如,我可能有一个URL,例如www.example.com/people/michael.phelps

带有点的url会生成404。我的路由是好的。如果我传入michaelphelps,没有点,那么一切正常。如果我加一个点,就会得到404错误。示例站点运行在Windows 7和IIS8 Express上。URLScan未运行。

我尝试在我的web.config中添加以下内容:

<security>
  <requestFiltering allowDoubleEscaping="true"/>
</security>

不幸的是,这并没有什么不同。我刚刚收到一个404.0未找到错误。

这是一个MVC4项目,但我不认为这是相关的。我的路由工作得很好,我所期望的参数都在那里,直到它们包含一个点。

我需要配置什么才能在我的URL中有圆点?


当前回答

只需将此部分添加到Web。所有对路由/{*pathInfo}的请求都将由指定的处理程序处理,即使在pathInfo中有点。(取自ServiceStack MVC主机Web。配置示例和这个答案https://stackoverflow.com/a/12151501/801189)

这应该适用于iis6和iis7。你可以通过修改'add'元素中的path="*"为'route'后面的不同路径分配特定的处理程序

  <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>

其他回答

我通过编辑我的站点的HTTP处理程序得到了这个工作。对于我的需要,这很好地解决了我的问题。

我只是添加了一个新的HTTP处理程序,用于查找特定的路径条件。如果请求匹配,则正确地将其发送到. net进行处理。我更喜欢这个解决方案,URLRewrite黑客或启用RAMMFAR。

例如,要让. net处理URL www.example.com/people/michael.phelps,请在站点的web中添加以下行。系统内配置。webServer / handlers元素:

<add name="ApiURIs-ISAPI-Integrated-4.0"
     path="/people/*"
     verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

Edit

还有其他帖子建议这个问题的解决方案是RAMMFAR或RunAllManagedModulesForAllRequests。启用此选项将为所有请求启用所有托管模块。这意味着静态文件,如图像、pdf文件和其他任何文件将在不需要时由. net处理。除非你有特定的情况,否则最好不要使用这个选项。

在一些闲逛后,我发现relaxedUrlToFileSystemMapping根本不为我工作,在我的情况下工作的是将RAMMFAR设置为true,同样适用于(.net 4.0 + mvc3)和(.net 4.5 + mvc4)。

<system.webserver>
    <modules runAllManagedModulesForAllRequests="true">

当设置RAMMFAR为true时,Hanselman发布了关于RAMMFAR和性能的帖子

我在这个问题上卡了很长一段时间,遵循了所有不同的补救措施,但都没有效果。

我注意到,当在包含点[.]的URL末尾添加正斜杠[/]时。),它没有抛出404错误,而且它确实工作了。

我最终解决了这个问题,使用一个URL重写器,如IIS URL重写,以观察一个特定的模式,并附加训练斜杠。

我的URL看起来是这样的:/Contact/~firstname。所以我的模式很简单:/Contact/~(.*[^/])$

我从Scott Forsyth那里得到了这个想法,见下面的链接: http://weblogs.asp.net/owscott/handing-mvc-paths-with-dots-in-the-path

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    [RoutePrefix("File")]
    [Route("{action=index}")]
    public class FileController : Controller
    {
        // GET: File
        public ActionResult Index()
        {
            return View();
        }

        [AllowAnonymous]
        [Route("Image/{extension?}/{filename}")]
        public ActionResult Image(string extension, string filename)
        {
            var dir = Server.MapPath("/app_data/images");

            var path = Path.Combine(dir, filename+"."+ (extension!=null?    extension:"jpg"));
           // var extension = filename.Substring(0,filename.LastIndexOf("."));

            return base.File(path, "image/jpeg");
        }
    }
}

MVC 5.0解决方案。

许多建议的答案似乎在MVC 5.0中不起作用。

由于上一节中的404点问题可以通过结尾的斜杠来解决,下面是我使用的小技巧,干净而简单。

同时在视图中保持一个方便的占位符:

@Html.ActionLink("Change your Town", "Manage", "GeoData", new { id = User.Identity.Name }, null)

添加一点jquery/javascript来完成工作:

<script>
    $('a:contains("Change your Town")').on("click", function (event) {
        event.preventDefault();
        window.location.href = '@Url.Action("Manage", "GeoData", new { id = User.Identity.Name })' + "/";
    });</script>

请注意后面的斜杠,它负责更改

http://localhost:51003/GeoData/Manage/user@foo.com

http://localhost:51003/GeoData/Manage/user@foo.com/