简体   繁体   English

C#MVC-路由问题/混乱

[英]C# MVC - Routing Issue / Confusion

I am developing one application in the ASP.NET MVC C# on the .NET 4 framework. 我正在.NET 4框架上的ASP.NET MVC C#开发一个应用程序。

I confused in routing and I do the research and developed the one demo version It works as I want but I want to know is which method is best practice for developing application. 我对routing感到困惑,我进行了研究并开发了一个演示版本。它可以按我的意愿工作,但我想知道哪种方法是开发应用程序的最佳实践。

First I register the route like this: 首先,我这样注册路线:

routes.MapRoute(
                name: "RoutesTesting",
                url: "{controller}/{action}/{a}/{b}/{c}/{d}/{e}",
                defaults: new { controller = "Home", action = "Test", e = UrlParameter.Optional }
                );

I have one class that have the properties and it's name same as the route's parameter. 我有一个具有属性的类,它的名称与路由的参数相同。

class MyClass{
    public string a{get;set;}
    public string b{get;set;}
    public string c{get;set;}
    public string d{get;set;}
    public string e{get;set;}
}

Now I created the tow methods that works find and get the data from the URL successfully. 现在,我创建了两个拖曳方法,可以成功地从URL查找和获取数据。

Method 1: 方法1:

public ActionResult Test(MyClass objMyClass){

}

Method 2: 方法2:

public ActionResult Test(string a,string b,string c,string d,string e=String.Empty){

}

My question is: 我的问题是:

  1. Is routing doing that conversation in my action method? 路由是使用我的操作方法进行对话吗? Like it convert the parameter values in the `MyClass' object's properties? 像它一样在“ MyClass”对象的属性中转换参数值吗?

  2. Which method is best practice to use? 最佳方法是使用哪种方法?

  3. Is method 1 will throw any error or exception when the conversation is not possible ? 当无法进行对话时,方法1是否会引发任何错误或异常?

Thanks in advance... 提前致谢...

Take a look at How model binding works 看看模型绑定如何工作的

Is routing doing that conversation in my action method? 路由是使用我的操作方法进行对话吗? Like it convert the parameter values in the `MyClass' object's properties? 像它一样在“ MyClass”对象的属性中转换参数值吗?

The framework model binder is doing the conversion based on the actions parameter. 框架模型绑定程序基于action参数进行转换。

Which method is best practice to use? 最佳方法是使用哪种方法?

That is an opinionated question. 这是一个自以为是的问题。 Depends on which one suits your needs. 取决于哪个适合您的需求。 The framework handles both. 该框架同时处理这两个问题。

Is method 1 will throw any error or exception when the conversation is not possible ? 当无法进行对话时,方法1是否会引发任何错误或异常?

Model binder will pass null to the action parameter for the properties that don't match. 模型资料夹会将不匹配的属性的null传递给action参数。

The behavior you are seeing is a part of ASP.NET's Model Binding. 您看到的行为是ASP.NET的模型绑定的一部分。 It's the magic that lets you send across a JSON object of {"firstName":"Jonathon","lastName":"Chase"} and have to automagically be mapped to a model Person that looks like so: 这是一种魔术,它使您可以跨{"firstName":"Jonathon","lastName":"Chase"}的JSON对象进行发送,并且必须自动映射到模型Person,如下所示:

public class Person {
    public string FirstName {get;set;}
    public string LastName {get;set;}
}

The fact that you can create a route like that is merely a consequence of this. 您可以创建这样的路线的事实仅仅是这样做的结果。 Model Binding is a complex subject, but I can touch on some aspects of how you're forming your route, especially if the action you're creating is going to have a side-effect, such as writing to a database. 模型绑定是一个复杂的主题,但是我可以谈谈您如何形成路线的某些方面,尤其是在您要创建的动作会产生副作用的情况下,例如写入数据库。

Typically if you're going to have a method that will effect state, you should use an Http verb other than Get, and send the model across in the body of the request, rather than in the query/url string. 通常,如果要使用一种会影响状态的方法,则应使用Get以外的Http动词,并在请求的正文中而不是在query / url字符串中发送模型。 The Model Binding will take care of the mapping for you either way. 无论哪种方式,模型绑定都会为您处理映射。

You should prefer to use a strong model rather than multiple primitives as parameters, especially in cases where the information will be sent in the body of a request over the query string. 您应该更喜欢使用强模型而不是多个原语作为参数,尤其是在信息将通过查询字符串在请求主体中发送的情况下。

These points are debatable, however, and shouldn't be considered hard or fast rules for the most part. 但是,这些观点尚有争议,在大多数情况下,不应将其视为硬性规定。

As to your last point, if the parameters are incorrect enough that the Route can't identifier the action or controller, you should get a 404. However, if you have a valuetype that isn't nullable as an expected routed property that isn't properly sent across, you should expect a 500 with an InvalidOperationException. 关于最后一点,如果参数不够正确,以致Route无法识别动作或控制器,则应获得404。但是,如果您的值类型不能作为期望的路由属性而不能为null,如果正确发送,您应该期望500带有InvalidOperationException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM