简体   繁体   English

如何重定向到特定的动作路线

[英]How to Redirect to specific action route

this is my custom route.but when I want to use RedirectToAction("action","controller");这是我的自定义路由。但是当我想使用RedirectToAction("action","controller"); it cause error.它导致错误。 No route in the route table matches the supplied values.路由表中没有路由与提供的值匹配。 I visited some related post but I could not solve it.我访问了一些相关的帖子,但我无法解决。 How do I can fix it?我该如何解决?

routes.MapMvcAttributeRoutes();
routes.LowercaseUrls = true;
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/{filter1}/{filter2}/{filter3}/{filter4}/{filter5}/{filter6}/{filter7}/{filter8}/{filter9}/",
    defaults: new
    {
        controller = "Shop",
        action = "Category",
        id = UrlParameter.Optional,
        filter1 = UrlParameter.Optional,
        filter2 = UrlParameter.Optional,
        filter3 = UrlParameter.Optional,
        filter4 = UrlParameter.Optional,
        filter5 = UrlParameter.Optional,
        filter6 = UrlParameter.Optional,
        filter7 = UrlParameter.Optional,
        filter8 = UrlParameter.Optional,
        filter9 = UrlParameter.Optional,
    }
);

I had a similar problem once with RedirectToAction and found out that you need a valid route registered that leads to that action.我曾经在RedirectToAction遇到过类似的问题,并发现您需要注册一个有效的路由来引导该操作。 in your case, the error occurred because your optional parameter is more than expected amount.在您的情况下,发生错误是因为您的可选参数超过预期数量。

I test code and it works :我测试代码,它的工作原理:

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

if you want your own route you can use [Route("")] on top of your Action, at the first Add routes.MapMvcAttributeRoutes();如果你想要你自己的路线,你可以在你的动作之上使用[Route("")] ,首先添加routes.MapMvcAttributeRoutes(); to RegisterRoutes method in RouteConfig.cs . RouteConfig.cs RegisterRoutes方法。 After that do like below:之后做如下:

[Route("{TestEmployee}/{Index}/{id?}/{filter1?}/{filter2?}/{filter3?}/{filter4?}/{filter5?}/{filter6?}/{filter7?}/{filter8?}/{filter9?}")]
public ActionResult Index()
{
    //do something 
}

then you can redirectToAction :然后你可以redirectToAction

return RedirectToAction("Index", "TestEmployee" ,routeValues: null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM