Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need a route defined for Html.Action?

I have created an otherwise empty ASP.NET MVC 3 application with 2 controllers, HomeController and OtherController.

HomeController.cs looks like this:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Index.cshtml looks like this:

@Html.Action("Index", "Other")

And, of course, Othercontroller.cs:

public class OtherController : Controller
{
    [ChildActionOnly]
    public ActionResult Index()
    {
        return Content("OK!");
    }
}

So far, so good. I run the app, and it tells me everything is OK!

Now, I take the default RegisterRoutes from Global.asax.cs:

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

And I crumple it up, so that no routes match OtherController:

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

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

Now, when I run the page, it crashes with this error message:

 System.InvalidOperationException: No route in the route table matches the supplied values.
Source Error:
Line 1:  @Html.Action("Index", "Other")

I specified the controller name and action name in the call to .Action. No URLs are being generated, and no requests are being made. Why does routing even need to get involved?

like image 834
Jacob Krall Avatar asked Jan 20 '12 01:01

Jacob Krall


People also ask

Why do we need routing in MVC?

Routing is not specific to the MVC framework. It can be used with ASP.NET Webform application or MVC application. ASP.NET introduced Routing to eliminate the needs of mapping each URL with a physical file. Routing enables us to define a URL pattern that maps to the request handler.

What is the use of route config?

The RouteConfig. cs file is used to set routing for the application. Initially it contains the following code.

How route is defined in MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).

What is route attribute in MVC?

Routing is how ASP.NET MVC matches a URI to an action. MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application.


1 Answers

I think that this blog post will help you understand a little more:

http://blogs.charteris.com/blogs/gopalk/archive/2009/01/20/how-does-asp-net-mvc-work.aspx.

Essentially, routing is involved in determining which controller to 'fire up' to handle the request and appropriate action to invoke based on the parameters that you sent and the MVCRouteHandler uses those to make a decision. Just because you tell it which controller in your action does not make it magically ignore the routing table, go straight to that controller class and bypass all the other MVC goodness that happens in the back-end. Remember, these @HTML.Action methods can take a whole lot of overloads which could influence which route in the routing table to use (think URL structure for instance).

The MVC paths are not to static content and as such, have to be parsed through the URLRoutingModule which uses the routing table to decide on what to do. Since you have no matching route - you get an error.

EDIT

In my diatribe, I did not actually address your final statement. You're right, no URL was generated, but a request to the application was generated. HTML.Action still will use routing to determine what controller, action, area, parameters to use. I think it be fair to say in simple terms that it's like generating an ActionLink and clicking it for you.

like image 80
Tommy Avatar answered Oct 13 '22 05:10

Tommy