开发者

ASP.net MVC RenderAction and Route

开发者 https://www.devze.com 2023-02-28 00:35 出处:网络
I have a web project using ASP.net MVC3. There\'s an child action in my project. I use <% Html.RenderAction(\"Navigator\", \"Application\");%>

I have a web project using ASP.net MVC3. There's an child action in my project. I use

<% Html.RenderAction("Navigator", "Application");%>

to call a shared action. But I find that if my current url is "localhost/application", it throws an exception "No route in the route table matches the supplied values". But when current url is "localhost/application/index", it works fine. Index is a default action in my route config, which is shown below:

    public static void RegisterRoutesTo(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
        //routes.IgnoreRoute("{*chartName}", new { chartName = @"(.*)?Chart.aspx" }); //ignore request for charting request
        routes.Ignore("{*pathInfo}", new { pathInfo = @"^.*(ChartImg.axd)$" });

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

Note that I switch default id and action posit开发者_C百科ion. I see mvc can remove the default controller and action name in url when using "Html.ActionLink(...)". And I don't like to use explicit url string in my views. How can make it work?

My Action code is simple:

    [ChildActionOnly]
    public ActionResult Navigator()
    {
        return PartialView(appFacility.GetAll());
    }

Thanks alot.


Wrong route URL definition and defaults combination

The problem is that you can't have non optional parameters after an optional parameter.

Why does localhost/application/index work? This are route values:

  • controller = "application" (supplied from URL)
  • id = "index" (supplied from URL)
  • action = "Index" (supplied as route default)

Basically these values equal to localhost/application/index/index request URL.

If you'd like your RenderAction to work, you'd have to call it this way:

<% Html.RenderAction("Navigator", "Application", new { id = 0 }); %>

which would equal to localhost/application/0/navigator request URL.

But you'll soon find out that your route doesn't work and you'll have to change it (because I suppose you don't like having that additional 0 in your URL). If you provide information how you'd like your route work (or why you've decided to switch action and id) we can provide an answer that will help you meet your requirements.


Optinal parameters work correctly only on the end of route. Try something like this:

routes.MapRoute("DefaultWithID", "{controller}/{id}/{action}",
    new { action = "Index" },
    new { id = "^[0-9]+$" }
);

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

edit: hopefully fixed :) this version counts on fact that ID will be numeric - without constraint we can't tell whether it would mean action or id, so there couldn't be default action on routes when ID is specified

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号