ASP.NET MVC路由冲突 - 输入变量空值 [英] ASP.NET MVC routing conflict - null value for input variable

查看:224
本文介绍了ASP.NET MVC路由冲突 - 输入变量空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知所措,为什么我的路线冲突。我有这些在我的Global.asax文件:

I'm at a loss as to why my routes are conflicting. I have these in my Global.asax file:

        routes.MapRoute(
        "CustomerView", "{controller}/{action}/{username}",
        new { controller = "Home", action = "Index", username = "" }
        );

        routes.MapRoute(
        "Default", "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = "0" }
        );

到目前为止,一切都除了当我创建了一个控制器动作,像这样的罚款:

So far everything has worked fine except when I created a controller action like so:

    public ActionResult MyAction(int id)
    {
        //Do stuff here
        return View();
    }

当我试图通过 HTTP查看它:// MYDOMAIN / myController的/ MyAction / 5 我得到:

参数字典包含
  为参数'ID'无效项
  非可空类型'System.Int32'的
  法System.Web.Mvc.ActionResult
  轨道(Int32)已在
  InTouch.Controllers.OrderController。
  为了使参数可选其类型
  应该是一个参考类型或
  可空类型。参数名:
  参数

Server Error in '/' Application.

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Track(Int32)' in 'InTouch.Controllers.OrderController'. To make a parameter optional its type should be either a reference type or a Nullable type. Parameter name: parameters

建议对我来说, ID 价值没有被正确读取。当然enoguh,当我交换路径的排列顺序围绕它工作正常。我的(当然有限)了解,到目前为止是,如果在路由中指定的变量名称相匹配,在控制器动作定义中指定,它会假设一个不分顺序。显然我错了。交换顺序导致其他控制器动作打破。什么是处理在这种情况下我路线的正确方法?

suggesting to me that the id value is not being read properly. Sure enoguh, when I swap the order of the routes around it works fine. My (admittedly limited) understanding so far was that, if a variable name specified in a route matches that specified in a controller action definition, it will assume that one regardless of order. Apparently I was wrong. Swapping the order causes other controller actions to break. What is the right way to handle my routes in this instance?

推荐答案

你的榜样的问题是,这场比赛发生在第一路线上,它看到5作为用户名参数。您可以使用约束来限制值,接受每个参数来完成你想要什么。由于接受一个I​​D的默认的路线是比CustomerView的路线更严格,我会首先列出了默认的路线与Id参数约束:

The problem with your example is that the match is happening on the first route and it's seeing "5" as the username parameter. You can use constraints to limit what values are accepted for each parameter to accomplish what you're wanting. Since the "Default" route that accepts an Id is more restrictive than the "CustomerView" route, I would list the "Default" route first with a constraint on the Id parameter:

routes.MapRoute(
    "Default", "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "0" },
    new { id = @"\d+" } 
    );

此将导致仅匹配所述第一路由,当ID是一个整数值。然后,所有其他请求将告吹了CustomerView路线,将拿起一个没有整数作为第三个参数的任何其他要求。

This will cause the first route to only match if Id is an integer value. All other requests would then fall through to the "CustomerView" route that would pick up any other requests that didn't have integers as that third parameter.

查看<一个href=\"http://www.asp.net/%28S%28pdfrohu0ajmwt445fanvj2r3%29%29/learn/mvc/tutorial-24-cs.aspx\">Creating一个路由约束获取约束的解释。

这篇关于ASP.NET MVC路由冲突 - 输入变量空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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