首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mvc:路由配置排序路由

Mvc:路由配置排序路由
EN

Stack Overflow用户
提问于 2013-04-17 11:40:53
回答 2查看 13.7K关注 0票数 3

我添加了一个自定义路由,如下所示

代码语言:javascript
运行
复制
        routes.MapRoute(
          name: "Default",
          url: "{coutry}/{lang}/{controller}/{action}",
          defaults: new { controller = "Home", action = "Index" }
        );

现在,当我试图从一个控制器调用一个方法时,我遇到了一些问题,在添加新的路由之前,它工作得很好。

代码语言:javascript
运行
复制
<a id="someId" class="link-button" href="../Documents/Create"><span>Create</span></a>

现在,我唯一能做到这一点的方法就是使用像href="EN/us/Documents/Create"这样的东西

有没有办法为我的客户端保留自定义路由,并且仍然为我的管理员端保留href="../Documents/Create">方式,这是因为我在管理员端开发了几个功能,但现在我必须包括客户端的自定义路由。非常感谢。

现在有我的路线了

代码语言:javascript
运行
复制
 routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );

routes.MapRoute(
            name: "CustomRoute",
            url: "{country}/{lang}/{controller}/{action}",
            defaults: new { controller = "Test", action = "Index" }
        );

但我只能使用/es/es/测试/索引访问CustomRoute ...为什么不采用默认值?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-17 11:48:17

您只需要在默认路由后声明您的自定义路由

代码语言:javascript
运行
复制
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index",
        id = UrlParameter.Optional }
);
// SomeOther = someothercontroller
routes.MapRoute(
    name: "CustomRoute",
    url: "{coutry}/{lang}/{controller}/{action}",
    defaults: new { controller = "SomeOther", action = "Index" }
);
票数 2
EN

Stack Overflow用户

发布于 2013-04-17 11:52:25

您已将默认Url替换为新配置,并且它确实与此{coutry}/{lang}/{controller}/{action}格式的RouteConfig相匹配。

如果您想要接受../Documents/Create Url,则必须在末尾添加默认RouteConfig。

代码语言:javascript
运行
复制
routes.MapRoute(
  name: "CustomRoute",
  url: "{coutry}/{lang}/{controller}/{action}",
  defaults: new { controller = "Documents", action = "Index" }
);

routes.MapRoute(
  name: "Default",
  url: "{controller}/{action}/{id}",
  defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

此外,在锚标记<a id="someId" class="link-button" href="../Documents/Create"><span>Create</span></a>中,您可以编写如下代码,而不是硬编码href

<a id="someId" class="link-button" href="@Url.Action("Create","Documents")><span>Create</span></a>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16051061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档