我正在使用MVC3,在我看来,我有以下代码块;
Response.Redirect(@Url.Action("index","home", new {error = "test"}));
所以我希望的网址是http://marketurk.co.uk/?error=test,但我得到的是http://marketurk.co.uk/%3Ferror=test,这样我就会遇到危险的请求。
有没有人有同样的问题,可以告诉我我做错了什么?
的更新: 的 我将逻辑移动到控制器方法中,如下所示;
public ActionResult Index()
{
if (!User.Identity.IsAuthenticated)
return RedirectToAction("index", "home", new { error = "test" });
return View();
}
它仍然会重定向到 http://marketurk.co.uk/%3Ferror=test
答案 0 :(得分:3)
这通常是进入我所说的控制器的逻辑。存在一条永远存在的不成文规则,即您的视图应包含尽可能少的逻辑,除非它用于演示目的。
在您的控制器操作中,您可以验证您的视图模型,如果它无效,您应该使用内置的RedirectToAction:
public ActionResult SomeAction()
{
// build your viewmodel here //
if(/* viewmodel is invalid */)
return RedirectToAction("index", "Home", new { error = "test" });
else
return View( /* your viewmodel */);
}
答案 1 :(得分:1)
答案应该是:
由?
编码为%3F
,
使用Html.Raw
,您可以保留?
Response.Redirect(Html.Raw(Url.Action("index","home", new {error = "test"})));
答案 2 :(得分:0)
您是否为该控制器使用OnActionExecuting功能?可能是您的请求后由另一行代码引起的问题?