Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4asp.net-mvc-routing

C# MVC - Routing Issue / Confusion


I am developing one application in the ASP.NET MVC C# on the .NET 4 framework.

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.

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.

Method 1:

public ActionResult Test(MyClass objMyClass){

}

Method 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?

  2. Which method is best practice to use?

  3. Is method 1 will throw any error or exception when the conversation is not possible ?

Thanks in advance...


Solution

  • The behavior you are seeing is a part of ASP.NET's Model Binding. 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:

    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. 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.