简体   繁体   中英

RenderAction in _layout gives “No route in the route table matches the supplied values.”

On my _layout.cshtml I have the following line

Html.RenderAction("ChamberComment", "Carnets", new { Area = "Chamber"});

This gives the following error:

No route in the route table matches the supplied values.

I am using routedebugger, so under the page that displays this error, I can actually see the matching route being found:

在此处输入图片说明

I have this route defined using AttributeRouting:

namespace MyProject.Net.Site.Areas.Chamber.Controllers
{
    [RouteArea("Chamber", AreaPrefix = "")]
    [AuthorizeRoles(Roles = RoleDiscriminator.Chamber)]
    public class CarnetsController : BaseController
    {
        [HttpGet]
        [Route("chamber/carnets/{slug}/{step}/chamberRemarks")]
        public ActionResult ChamberComment(string slug, string step)
        {

I can also go straight to the URL

http://myproject/chamber/carnets/18-reffr/general/chamberRemarks

and it renders the partial in the browser, with underneath the expected RouteDebugger information...

So somehow the system is unable to translate the RenderAction statement to the correct action when used in the _layout masterpage, but it is able to route the specified url to this action..

Any idea in which direction I should be looking?

A related problem occurs if you have the sub action in a top-level controller and you try to render it from within a layout. In my case, this occurred in a project that had areas. I'm not sure if it also occurs in projects without areas.

I have a default route which should satisfy the [Render]Action parameters. However, this case was always failing:

Html.Action("ActionName", "ControllerName")

The solution was to add a null area:

Html.Action("ActionName", "ControllerName", new { area = "" })

Now, the exception no longer occurs and the partial view is rendered.

Have you tried providing the slug and step parameters as in:

 Html.RenderAction("ChamberComment", "Carnets", new { Area = "Chamber", slug = "18-reffr", step = "general"});

If those parameters are not in the current Url being rendered, you will need to manually pass them to the Html.RenderAction method.

As per your comment, the slug is found in the current Url but step is hardcoded so you will need to provide it to the helper.

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