简体   繁体   中英

Html.RenderAction with Route Name

I am trying to utilize the Route attribute naming (ex: [Route(Name = "Abc123")] ) in a controller action, and likewise be able to call that via Html.RenderAction , but it appears that this does not support route names. I assume this is because route names are reserved only for ones requested via HTTP and not called directly, but somewhat new to MVC so I'm at a loss.

I'm using MVC Attribute Routing entirely, and I do not have routes configured otherwise. It seems that I must define the route name, and the route name has to match the action method name. In doing so, however, I am getting naming conflicts when I try to name more than one Index .

I'm basically trying to support multiple partial views, each having their own controller, which serve as plugins/widgets on my site. So ideally each would have an action called Index .

Do you have a recommendation on how I can maintain the same naming? This allows me to call Html.RenderAction("Index", [ControllerName], [Model]) without the render name changing.

You can use attribute routing with Html.RenderAction just you have to make sure that the action name in attribute is actual Name :

 [Route("Home/About")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

and in HTML you can have

@{Html.RenderAction("About", "home");}

It will work fine.

You should be able to do what you are trying to do with attribute routing. If you are trying to render a partial view you should be using RenderPartial instead of RenderAction.

Are you doing this between areas? I believe there are some gotcha's with making that work correctly.

Can you please post example Controller code and RouteConfig?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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