Html.RenderAction with Route Name

徘徊边缘 提交于 2020-02-24 10:53:46

问题


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.


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/30382953/html-renderaction-with-route-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!