【问题标题】:MVC Route Configuration: Multiple route with different route nameMVC 路由配置:具有不同路由名称的多个路由
【发布时间】:2016-01-17 12:16:36
【问题描述】:

我在 MVC 中创建了多个具有不同路由名称的路由。

routes.MapRoute(
    name: "PostDetails",
    url: "Ad/{id}/{item}",
    defaults: new { controller = "Home", action = "Post" }
);

我正在从 javascript 函数调用路由以重定向到该路由

var url = '@Url.RouteUrl("PostDetails", new { id = "_id_", item = "_name_" })';     
url = url.replace("_id_", id).replace("_name_", name);
window.location.href = url;

这给出了 404 错误。

【问题讨论】:

  • 这个脚本是在视图中还是在外部js文件中?
  • 您的操作应该是 - public ActionResult Post(string id, string item) 。我添加了string name 作为参数之一来匹配您的路线。试试这种方式,让我知道它是否有效。
  • 这条路线之前的routeconfig.cs还有其他路线吗?
  • 你能用控制器代码编辑你的问题吗?
  • 请发布您的路线和控制器代码。这不是一个有效的问题,除非它可以被其他人复制,并且您到目前为止发布的代码显然不是您问题的根本原因。为你的问题付出一些努力,你将更有可能得到有用的答案。

标签: asp.net-mvc routing asp.net-mvc-5


【解决方案1】:

您必须在 RouteConfig 中的默认路由之前添加您的路由,例如:

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "PostDetails",
            url: "Ad/{id}/{item}",
            defaults: new { controller = "Home", action = "Post" }
        );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
}

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 1970-01-01
    • 2014-09-11
    • 2017-12-03
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2012-07-13
    相关资源
    最近更新 更多