Cal*_*min 3 .net asp.net-mvc redirect asp.net-mvc-4
我有一个操作结果方法,里面正在执行重定向(url)。我的问题是如何在执行重定向之前检查网址是否有效?
public ActionResult RedirectUser()
{
var url = "/Cars/Model/1"; //this is the url
// now i should check if the redirect return a 200 code (the url is valid) and if is valid I should redirect to that url, else i should redirect to "/Home/Index"
if(this.Redirect(url))
{
return this.Redirect(url);
}
else
{
return this.RedirectToAction("Index", "Home");
}
return this.RedirectToAction("Index", "Home");
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我举个例子吗?我在谷歌上搜索,但找不到任何可以帮助我的东西。谢谢
尝试这个
public ActionResult RedirectUser()
{
var url = "/Cars/Model/1"; //this is the url
var controller = RouteData.Values["controller"].ToString();
var action = RouteData.Values["action"].ToString();
if(controller=="car"&& action=="Model")
{
return this.Redirect(url);
}
else
{
return this.RedirectToAction("Index", "Home");
}
return this.RedirectToAction("Index", "Home");
}
Run Code Online (Sandbox Code Playgroud)