MVC路由问题:无效项 [英] MVC Routing issue: null entry

查看:154
本文介绍了MVC路由问题:无效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的控制器:

public class TestController : Controller
{
    // GET: Test
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Edit(int accessLevel)
    {
        return View();
    }
}

在RouteConfig.cs设置为:

Set up in RouteConfig.cs as:

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

routes.MapRoute(
    name: "Test Edit",
    url: "Test/Edit/{accessLevel}",
    defaults: new { controller = "Test", action = "Edit", accessLevel = UrlParameter.Optional }
);

如果我去这个网址:

http://localhost:35689/Test/Edit/2

我得到这个错误:

I get this error:

参数词典共包含参数无效项
  非可空类型'System.Int32'的'ACCESSLEVEL'的方法
  System.Web.Mvc.ActionResult编辑(Int32)已在
  MyProject.Mvc.Client.Controllers.TestController。可选
  参数必须是引用类型,可空类型,或声明为
  一个可选参数。参数名:参数

The parameters dictionary contains a null entry for parameter 'accessLevel' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'MyProject.Mvc.Client.Controllers.TestController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

任何想法,这是为什么?我会认为我提供正确的数据类型与 / 2

Any idea why that is? I would think that I'm providing the right datatype with /2.

推荐答案

的具体路线定义应该一般默认的之前定义。的路由定义的顺序真正重要

The specific route definition should be defined before the generic default one.The order of route definitions really matters.

routes.MapRoute(
    name: "Test Edit",
    url: "Test/Edit/{accessLevel}",
    defaults: new { controller = "Test", action = "Edit", 
                                                    accessLevel = UrlParameter.Optional }
);

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

如果您保留其他命令就像你有什么(一般默认第一,具体的一个版本),当收到请求时为测试/编辑/ 2 这将是相匹配的通用路由定义,因为测试是一个有效的控制器和修改是一个有效的操作方法名称, 2 可能是标识参数有效的参数值。
由于该请求得到了有效的路由定义来匹配它的URL模式,它决不会反对下面的第一个定义的其他路由定义​​评估。

If you keep the other order like what you have (Generic-default first ,specific one later), When a request comes for Test/Edit/2 It will be matched to the generic route definition because Test is a valid controller and Edit is a valid action method name and 2 could be a valid param value for Id param. Since the request got a valid route definition to match to it's url pattern, It will never be evaluated against other route definitions defined below the first one.

请所有特定路由定义第一,有通用默认一个作为最后一只了。

Keep all specific route definitions first and have the generic-default one as the very last one.

或者,你可以使用路由属性来定义的测试controller.To此路由模式能属性的路由,可以调用在 MapMvcAttributeRoutes 方法>的RegisterRoutes RouteConfig.cs的方法。你仍然会保持默认的路由定义在那里。

Or You may use attribute routing to define this route pattern in the Test controller.To enable attribute routing, you can call the MapMvcAttributeRoutes method in the RegisterRoutes method of RouteConfig.cs. You will still keep the default route definition there.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

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

    );
}

和在的TestController

[Route("Test/Edit/{id?}")]
public ActionResult Edit(int? id)
{
   //check id value  and return something
}

此外,还有是如果它与一般的默认路由定义相匹配定义自定义路线没有意义的。在你的情况,即使你不定义自定义路由,测试/编辑/ 2 将转到修改 的TestController 作为请求的默认路由定义匹配的操作方法。

Also, there is no point in defining a custom route if it matches with the generic default route definition. In your case, Even if you do not define the custom route ,Test/Edit/2 will go to the Edit action method of TestController as the request matches the default route definition.

人们通常使用这些定制的路由定义来创建像

People usually use these custom route definition to create nice url patterns like

[Route("Product/{id}/{name}")]
public ActionResult View(int id,string name)
{
   //check id value  and return something
}

这个路由定义将匹配请求产品/ 34 / SEO友好的名称。在这个问题的URL看看,你就会明白我解释这里。

This route definition will match the request Product/34/seo-friendly-name. Take a look at the URL of this question and you will understand what i am explaining here.

这篇关于MVC路由问题:无效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆