3

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.

trnelson
  • 2,715
  • 2
  • 24
  • 40

2 Answers2

2

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.

0

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?

bechbd
  • 6,206
  • 3
  • 28
  • 47
  • Forgive me for being pretty new to MVC so this might be a dumb question. Each of the included plugins (that I'm rendering using RenderAction) currently has its own controller. This was based on a suggestion from someone else, so that each plugin could sort of live independently and have its own functionality. Is that the right approach, or is it overkill for what I'm doing? Consider that each plugin acts *something* like the widgets on PageFlakes/iGoogle/Netvibes, so could be a calendar, a twitter feed, some static HTML, an RSS feed, etc. – trnelson May 22 '15 at 01:36
  • [This post](http://stackoverflow.com/questions/719027/renderaction-renderpartial) mimics closely what I want to do, so it does seem that RenderAction might be the right approach. The problem I'm running into, then, is whether I can somehow use named routes (or another convention) to refer to the actions without any naming conflicts. – trnelson May 22 '15 at 02:07
  • If each plugin has it's own Controller as you mentioned then there should not be a problem with using RenderAction. Without more information it's hard to say if this is overkill or not but either way you can have a different action named "Index" in each Controller – bechbd May 22 '15 at 02:44