C# ASP.NET MVC重定向到操作重定向循环
我正在学习asp.NETMVC,我尝试重定向到操作,我尝试了下面的代码,我不断得到重定向循环错误。 这是控制器类,我在其中得到一个错误C# ASP.NET MVC重定向到操作重定向循环,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我正在学习asp.NETMVC,我尝试重定向到操作,我尝试了下面的代码,我不断得到重定向循环错误。 这是控制器类,我在其中得到一个错误 public class CuisineController : Controller { // GET: Cuisine public ActionResult Search(string name) { return RedirectToAction("About","Cuisine"); }
public class CuisineController : Controller
{
// GET: Cuisine
public ActionResult Search(string name)
{
return RedirectToAction("About","Cuisine");
}
public ActionResult Index()
{
return Content("This is Index");
}
public ActionResult About()
{
return Content("this is About");
}
}
我还创建了自己的路由,而不是默认路由
routes.MapRoute(name: "Cuisine",
url: "cuisine/{name}",
defaults: new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional });
当我尝试访问控制器时,它会给我一个重定向循环错误。
谢谢您在您的routeConfig中,您已经为“
美食/{name}
”定义了一条路线,它将被发送到搜索操作方法
在您的搜索
操作方法中,您将重定向到关于
,即关于
。这与您定义的路由courine/{name}
匹配,因此它将再次发送搜索操作请求。相同的进程将继续运行。这就是为什么你会得到重定向循环
您应该删除您定义的这个特定路由,或者重命名cusine搜索的url模式,以防止重定向循环
routes.MapRoute(name: "Cuisine",
url: "searchcuisine/{name}",
defaults: new { controller = "Cuisine", action = "Search",
name = UrlParameter.Optional });
在routeConfig中,您有一个为“
courine/{name}
”定义的路由,它将被发送到搜索操作方法
在您的搜索
操作方法中,您将重定向到关于
,即关于
。这与您定义的路由courine/{name}
匹配,因此它将再次发送搜索操作请求。相同的进程将继续运行。这就是为什么你会得到重定向循环
您应该删除您定义的这个特定路由,或者重命名cusine搜索的url模式,以防止重定向循环
routes.MapRoute(name: "Cuisine",
url: "searchcuisine/{name}",
defaults: new { controller = "Cuisine", action = "Search",
name = UrlParameter.Optional });
这应该很明显:
routes.MapRoute(name: "Cuisine",
url: "cuisine/{name}",
defaults: new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional })
使用CuisineController
上的Search
方法显示所有以cuisine/
开头的URL
/courine/About
以该url开头,因此它将始终使用搜索方法。这应该非常明显:
routes.MapRoute(name: "Cuisine",
url: "cuisine/{name}",
defaults: new { controller = "Cuisine", action = "Search", name = UrlParameter.Optional })
使用CuisineController
上的Search
方法显示所有以cuisine/
开头的URL
/courine/About
以该url开头,因此它将始终使用搜索方法。看起来您希望使{name}成为url的一部分。您可以使用属性路由而不是更改默认路由,它至少不会产生全局“破坏性”影响(在其他答案中解释),而且看起来它就是您真正想要的:)
请参阅有关属性路由的详细信息(要使其起作用,请不要忘记添加routes.mapmvcattributteroutes()
。看起来您希望将{name}作为URL的一部分。您可以使用属性路由而不是更改默认路由,它至少不会产生全局“破坏性”影响(在其他答案中解释),而且看起来它就是您真正想要的:)
请参阅有关属性路由的详细信息(要使其起作用,请不要忘记添加routes.mapmvcattributteroutes()
。您需要恢复控制器操作模式。只需将url:“courine/{name}”
更改为url:“courine/{action}/{name}”
您需要恢复控制器操作模式。只需将url:“美食/{name}”
更改为url:“美食/{action}/{name}”