我正在创建一个友好的URL,所以我在routeconfig文件中有克里特路由,如下所示
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ShowUser", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
和行动链接如下
<a href="@Url.Action( "Index", "ShowUser")" onclick="javascript:onBegin()">View</a>
但是它没有工作给出运行时错误“无法找到资源。”
答案 0 :(得分:2)
默认路由和您的自定义路由具有相同的结构,因此将始终使用默认路由,因为它是首先声明的。 我猜你得到错误,因为在代码中你指的是ShowUser,而是你应该使用控制器的名称:UserGroupEdit。
<a href="@Url.Action( "Index", "UserGroupEdit")" onclick="javascript:onBegin()">View</a>
答案 1 :(得分:1)
如果你想要一个友好的路线,你可以做这样的事情
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>all-jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<useTransitiveDependencies>false</useTransitiveDependencies>
<includes>
<include>com.test.project:client</include>
</includes>
</dependencySet>
在public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ShowUser", // Route name
url: "ShowUser/index/{id}", // URL with parameters
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
}
cshtml
答案 2 :(得分:0)
您可以使用UrlHelper.RouteUrl
帮助程序方法来指定路径名称:
<a href="@Url.RouteUrl("ShowUser", new { action = "Index" })" onclick="javascript:onBegin()">View</a>
RouteConfig:
routes.MapRoute(
name: "ShowUser",
url: "ShowUser/{action}/{id}",
defaults: new { controller = "UserGroupEdit", action = "Index", id = UrlParameter.Optional }
);
参考link