ASP.NET MVC属性路由没有命中正确的控制器

问题描述 投票:0回答:3

我有几个像这样的控制器:

[RoutePrefix("side-navigation")]
public class SideNavigationController : BaseController
{
    [Route("{pathname}")]
    public ActionResult Index(string pathname)
    {
        SideNavigationPopoutModel model = _sideNavFactory.Value.CreatePopout(pathname);

        if (model != null)
        {
            return View(model);
        }

        return HttpNotFound();
    }
}

public class CatchAllController : BaseController
{
    public ActionResult Index(string pathname)
    {
        CatchAllModel model = _catchAllModelFactory.Value.Create(pathname);

        if (model != null)
        {
            // TODO: Do we need this - what does it do?
            // TempData.Restore(this);

            return View(model);
        }

        return HttpNotFound();
    }
}

但是我似乎无法在侧面导航控制器中进行我的索引操作 - 如果我浏览到localhost/side-navigation/test它会使用side-navigation/test捕获所有控制器作为路径名而不是使用test作为路径名的侧导航。

任何人都可以看到我在这里做错了什么或如何使侧导航控制器工作?

这是路由配置:

// MVC attribute routing
routes.MapMvcAttributeRoutes();

// Default catch all route
routes.MapRoute(
    "Default",
    "{*pathname}",
    new { controller = "CatchAll", action = "Index" });

奇怪的是,如果我将侧导航索引的路线更改为test/{pathname}并浏览到side-navigation/test/test它将起作用并且控制器将被命中但我不想在路径名之前添加任何内容

c# asp.net-mvc routes asp.net-mvc-5.2
3个回答
0
投票

看来你没有使用[Area]也把属性[Route(“[action]”)]放在上面的方法上。


0
投票

好的我已通过在路径名前添加星号来修复此问题:

[RoutePrefix("side-navigation")]
public class SideNavigationController : BaseController
{
    [Route("{*pathname}")]
    public ActionResult Index(string pathname)
    {
    }
}

如果有人能够解释为什么这样做并且没有星号没有,那么我将非常感激,因为我也有一个产品控制器设置方式完全相同,不需要星号


-3
投票

[RoutePrefix(“side-navigation”)] public class SideNavigationController:BaseController {[Route(“{* pathname}”)] public ActionResult Index(string pathname){}}

© www.soinside.com 2019 - 2024. All rights reserved.