2011-12-17 50 views
1

我使用的是MVC3,在我看來我有下面的代碼塊;爲什麼在重定向獲取「潛在危險的請求」時使用包含問號的URL?

Response.Redirect(@Url.Action("index","home", new {error = "test"})); 

,所以我期望的URL爲http://marketurk.co.uk/?error=test,但我得到的是這種http://marketurk.co.uk/%3Ferror=test,使我得到潛在危險的請求。

沒有人有同樣的問題,並可以告訴我我做錯了什麼?

UPDATE: 我移動邏輯到控制器的方法如下:

public ActionResult Index() 
    { 

     if (!User.Identity.IsAuthenticated) 
      return RedirectToAction("index", "home", new { error = "test" }); 

     return View(); 
    } 

它仍然重定向到http://marketurk.co.uk/%3Ferror=test

+0

此行// Url.Action(「index」,「home」,new {error =「test」})在重定向之外產生了什麼? – gdoron 2011-12-17 21:27:48

+0

我得到這個/?error = test – Cem 2011-12-18 00:29:58

回答

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 */); 
} 
+0

+1絕對控制器代碼 – 2011-12-17 22:13:38

+0

我已經完成了,它仍然重定向到包含%3F的相同url – Cem 2011-12-18 00:36:29

1

答案應該是:
?通過編碼爲%3F, 使用Html.Raw您可以在您使用OnActionExecuting功能FO保持?

Response.Redirect(Html.Raw(Url.Action("index","home", new {error = "test"}))); 
0

那控制器?您的請求後可能是由另一行代碼引起的問題?

相關問題