【问题标题】:MVC action not getting calledMVC 动作没有被调用
【发布时间】:2014-07-07 20:40:00
【问题描述】:

我只有 1 个控制器,并且仅在我的示例中具有 1 个操作,如下所示:

//
// GET: /Home/
public ActionResult Index(string source,string id)
{
    return View();
}

我为此操作注册了 2 条路线,例如 -:

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

当我调用默认时,它调用Index 动作,这没关系 当我这样打电话时,/source1/12 - 它有效。

但是当我像这样/source1/12.0 打电话时 - 它不起作用并显示 404 ..

谁能指出为什么会这样?

【问题讨论】:

  • 你能显示源动作吗?
  • 这是因为你的行为会像public ActionResult Source(int id)
  • public ActionResult Index(string source,string id) { return View(); }
  • 唯一的区别是当我像 /sourceId/12 一样调用时 - 它可以工作,但使用 /sourceId/12.0 - 它不(12.0)不工作,而 /sourceid/12.0/ 工作(使用 / in最后)
  • 可能是因为点(.)

标签: asp.net-mvc


【解决方案1】:

它可能将 .0 解释为文件扩展名,从而在磁盘上查找文件。能不能换成“。”使用“_”进行重定向,并在您的操作方法中替换回来?否则,您必须考虑如何让路由不将“.0”解释为文件扩展名。不知道我的头顶到底是怎么回事......

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

【讨论】:

    【解决方案2】:

    查看this的文章,它有适合你的解决方案:

    来自链接:

    <configuration>
      <system.web>
        <httpRuntime relaxedUrlToFileSystemMapping="true"/>
    
        <!-- ... your other settings ... -->
      </system.web>
    </configuration>
    

    【讨论】:

      【解决方案3】:

      将路由配置更改为:

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

      应该可以解决您的问题。

      你也可以查看这个SO Thread

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-26
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 2016-12-03
        • 1970-01-01
        相关资源
        最近更新 更多