当动作包含路由属性时如何重定向到动作
How to redirect to action when action contains route attribute
控制器
public class TestController{
[Route("TestIndex")]
public ActionResult Index(){
}
}
html
<a href="@Url.Action("Index","TestController")">Go to test</a>
我有上面的代码!!
在转到测试时单击我想重定向到 Testcontroller 的索引操作,因为我使用了下面的代码
href="@Url.Action("Index","TestController")"
但我认为因为在那里使用了路由属性,所以它没有重定向到该操作
提前致谢
你的代码中有错误,应该是
<a href="@Url.Action("Index","Test")">Go to test</a>
或者也许
<a href="@Url.Action("TestIndex","Test")">Go to test</a>
你必须使用一种方式——绝对路径,或者相对路径。由于你没有 post 控制器路由和你的配置端点,我无法理解你真正需要什么路由,但试试这个,它肯定会工作
<a href="~/test/testIndex">Go to test</a>
和路线
[Route("~/Test/TestIndex")]
public ActionResult Index(){
}
ASP.NET 为此内置了一些方法,例如 RedirectToAction
请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2
用法如下:
return RedirectToAction("controller", "method");
要传递路由属性,只需使用:
return RedirectToAction("controller","method", new { id });
如果这不起作用,请尝试使用:
RedirectToAction with parameter
return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "Main", Id = Id } ) );
有些人注意到另一个例子对他们不起作用。
控制器
public class TestController{
[Route("TestIndex")]
public ActionResult Index(){
}
}
html
<a href="@Url.Action("Index","TestController")">Go to test</a>
我有上面的代码!!
在转到测试时单击我想重定向到 Testcontroller 的索引操作,因为我使用了下面的代码
href="@Url.Action("Index","TestController")"
但我认为因为在那里使用了路由属性,所以它没有重定向到该操作
提前致谢
你的代码中有错误,应该是
<a href="@Url.Action("Index","Test")">Go to test</a>
或者也许
<a href="@Url.Action("TestIndex","Test")">Go to test</a>
你必须使用一种方式——绝对路径,或者相对路径。由于你没有 post 控制器路由和你的配置端点,我无法理解你真正需要什么路由,但试试这个,它肯定会工作
<a href="~/test/testIndex">Go to test</a>
和路线
[Route("~/Test/TestIndex")]
public ActionResult Index(){
}
ASP.NET 为此内置了一些方法,例如 RedirectToAction
请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2
用法如下:
return RedirectToAction("controller", "method");
要传递路由属性,只需使用:
return RedirectToAction("controller","method", new { id });
如果这不起作用,请尝试使用: RedirectToAction with parameter
return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "Main", Id = Id } ) );
有些人注意到另一个例子对他们不起作用。