无法路由到“{controller} / {action} / {id}”和“{controller} / {id}”

时间:2016-08-31 11:47:41

标签: c# asp.net asp.net-mvc razor

在我的MVC网站上,我希望我的路线设置如下:

  • site.com/Customer - 您可以选择客户。

  • site.com/Customer/123 - 您可以在其中查看所选客户的信息。

所以基本上,只有一个视图,取决于你是否有{id}显示不同的东西。

所以我在DefaultNoAction中添加了映射的路由RouteConfig.cs

// Route definitions.
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

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

        routes.MapRoute(
            name: "Parameters2",
            url: "{controller}/{action}/{id1}/{id2}",
            defaults: new { controller = "Home", action = "Index", id1 = UrlParameter.Optional, id2 = UrlParameter.Optional }
        );

控制器中的以下内容:

public ActionResult Index(int? id)
{
    return View(id);
}

和视图:

@model int?

...

$(function () {
    var customerId = @Model;
    ...
}

现在:

  • 如果我尝试访问site.com/Customer/123,我会收到404。
  • 如果我转到site.com/Customer @Model未设置为任何内容,那么当我在jQuery中引用它时会抛出语法错误,因为它会看到var customerId = ;

显然,我没有以正确的方式接近这个,或者有更好的方法来做我想做的事情。任何方向?

3 个答案:

答案 0 :(得分:3)

The order of your routes matter (the routing engine will stop searching when it finds the first match). ../Customer/123 matches you Default route (it contains 2 segments) and you get a 404 because there is no method named 123 in your CustomerController. You need to move the DefaultNoAction route before the Default route.

However this may cause other issues with your routing and you should make the DefaultNoAction route unique if you also want to use the Default route

routes.MapRoute(
    name: "DefaultNoAction",
    url: "Customer/{id}",
    defaults: new { controller = "Customer", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

As for the javascript error, assuming you want the value of customerId to be null if the methods id parameter is null, then you can use

var customerId = @Html.Raw(Json..Encode(Model));
console.log(customerId); // returns null if the model is null

As a side note, your Parameters2 route will not work correctly either. Only the last parameter can be marked as Url.Optional, and if only one or the 2 parameters were provided it would match the Default route.

答案 1 :(得分:2)

使用属性路由

[Route("api/Customer/{id:int?}")]
public ActionResult Index(int? id)
{
    if (id!=null)     
      return View(id);
    return View();
}

答案 2 :(得分:1)

Change order in route config. Second configuration should be first. I don't check but I think that framework interprets url site.com/Customer/123 like Customer is controller and 123 is the name of action.

View should looks like:

@model int?

...

$(function () {
    var customerId = @(Model ?? 0);
    ...
}