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


当前回答

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

其他回答

添加URL重写规则到Web。配置档案。您需要在IIS中已经安装URL重写模块。使用下面的重写规则作为你自己的灵感。

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Add trailing slash for some URLs" stopProcessing="true">
        <match url="^(.*(\.).+[^\/])$" />
          <conditions>
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Redirect" url="{R:1}/" />
      </rule>
    </rules>
    </rewrite>
</system.webServer>

</configuration> 

这是我在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环境下工作得很好。

尝试了上面所有的解决方案,但没有一个对我有效。我卸载了。net版本> 4.5,包括它所有的多语言版本;最后,我一点一点地添加了更新的(只有英文)版本。现在安装在我系统上的版本是这样的:

2.0 3.0 3.5 4 4.5 4.5.1 4.5.2 4.6 4.6.1

在这一点上它还在工作。我害怕安装4.6.2,因为它可能会把一切都搞砸。

因此,我只能推测是4.6.2或所有那些非英文版本把我的配置弄乱了。

我通过编辑我的站点的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和性能的帖子