我有一个项目,要求我的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中有圆点?


当前回答

我能够解决这个问题的特定版本(必须使/customer.html路由到/customer,不允许拖尾斜杠)使用https://stackoverflow.com/a/13082446/1454265的解决方案,并替换path="*.html"。

其他回答

这就像将path="."更改为path=""一样简单。只需在web.config中删除ExensionlessUrlHandler-Integrated-4.0路径中的圆点。

这是一篇不错的文章https://weblog.west-wind.com/posts/2015/Nov/13/Serving-URLs-with-File-Extensions-in-an-ASPNET-MVC-Application

我通过编辑我的站点的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处理。除非你有特定的情况,否则最好不要使用这个选项。

这是我在iis7.5和. net Framework 4.5环境中发现的404错误的最佳解决方案,并且不使用:runAllManagedModulesForAllRequests="true"。

我关注了这个帖子:https://forums.asp.net/t/2070064.aspx?Web+API+2+URL+routing+404+error+on+IIS+7+5+IIS+Express+works+fine,我已经修改了我的网站。现在MVC web应用程序在iis7.5和. net Framework 4.5环境下工作得很好。

此外,(相关的)检查处理程序映射的顺序。我们在.ashx后面的路径中有一个.svc(例如/foo.asmx/bar.svc/path)。.svc映射首先是。svc路径,因此在。asmx之前匹配的。svc路径是404。 没有想太多,但也许url编码的路径会照顾到这一点。

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");
        }
    }
}