Asp.net mvc MVC操作未被调用

Asp.net mvc MVC操作未被调用,asp.net-mvc,Asp.net Mvc,在我的示例中,我只有一个控制器和一个操作,如下所示: // // GET: /Home/ public ActionResult Index(string source,string id) { return View(); } 我已为此操作注册了两条路线,如-: routes.MapRoute( name: "Default2", url: "{source}/{id}", defaul

在我的示例中,我只有一个控制器和一个操作,如下所示:

//
// GET: /Home/
public ActionResult Index(string source,string id)
{
    return View();
}
我已为此操作注册了两条路线,如-:

 routes.MapRoute(
                name: "Default2",
                url: "{source}/{id}",
                defaults: new { controller = "Home", action = "Index" }
            );

            routes.MapRoute(
                name: "Default",
                url: "",
                defaults: new { controller = "Home", action = "Index", source="source1", id = UrlParameter.Optional }
            );
当我调用default时,它调用Index操作,这是正常的 当我这样调用时,/source1/12-它可以工作

但是当我像这样调用/source1/12.0时,它不工作,显示404


有人能解释为什么会发生这种情况吗?

它可能将.0解释为文件扩展名,从而在磁盘上查找文件。你能替换吗。使用u进行重定向,并在操作方法中替换回?否则,您必须考虑如何使路由不将.0解释为文件扩展名。我不知道我脑子里到底是怎么想的

MVC将这样的错误路由到特殊的方法。一个在控制器中:HandleUnknownAction,当操作无法匹配时调用它。您可以覆盖它并在那里处理记录它的操作等。

检查文章,它为您提供了一个解决方案:

从链接:

<configuration>
  <system.web>
    <httpRuntime relaxedUrlToFileSystemMapping="true"/>

    <!-- ... your other settings ... -->
  </system.web>
</configuration>

将ROuteCOnfig更改为:

routes.MapRoute(
            name: "Default2",
            url: "{source}/{id}/",
            defaults: new { controller = "Home", action = "Index" }
        );
应该能解决你的问题


你也可以检查这个

你能显示源操作吗?这是因为你的操作将类似于public ActionResult Sourceint idpublic ActionResult Indexstring source,string id{return View;}唯一的区别是当我调用like/sourceId/12时,它可以工作,但与/sourceId/12.0一起工作-它不工作,12.0不工作,虽然/sourceid/12.0/与/last一起工作,但由于点的原因,它可能是b。