2012-01-14 42 views
0

我從應用程序拋出404錯誤。
404錯誤的頁面位於/ErrorPages/Error_404.cshtml 在這個文件中,我只有HTML代碼,它工作正常。
但是,如果我添加一些剃鬚刀代碼,它會在瀏覽器中引發配置錯誤。
我添加例如佈局或一些@Html.ActionLink(...
這是錯誤:在cshtml(錯誤)頁中的剃刀代碼產生錯誤

Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

Details: To enable the details of this specific error message to be viewable on the local server machine, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".

我這是怎麼產生404:

public ActionResult Search(string searchTerm) 
{ 
if (city == null) 
      { 
       throw new HttpException(404, "Some description");    
      } 
      else 
      { 
       return RedirectToAction("Index", "Home", new 
       {... 
      } 
} 

當有錯誤頁面沒有剃刀代碼,它作品,如果沒有,我從上面得到消息。
當我在網絡配置設置「= OFF」,然後我得到錯誤信息:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /CityPage/Navigation/Search

這是從Web配置價值

<customErrors mode="Off"> 
     <error statusCode="404" redirect="\ErrorPages\Error_cp404.cshtml" /> 
    </customErrors> 
+2

設置自定義錯誤爲關在web.config中,告訴我們實際的錯誤信息。 – Judo 2012-01-14 01:35:57

+0

@Judo如果他設置了自定義錯誤,系統將不會呈現他的錯誤消息頁面,但顯示默認的異常堆棧。 1110,你應該調試該頁面,並告訴我們實際的錯誤 – archil 2012-01-14 12:35:52

+0

我根據你的意見更新了我的問題。 – 1110 2012-01-14 14:23:55

回答

4

你不應該試圖呈現.cshtml頁直。這些是Razor的觀點。在ASP.NET MVC視圖通過控制器操作提供服務。

所以:

<error statusCode="404" redirect="Errors/Error404" /> 

在那裏你可以有一個ErrorsController:

public class ErrorsController: Controller 
{ 
    public ActionResult Error404() 
    { 
     return View(); 
    } 
} 

您還可以檢出following answer一種替代的方式來處理錯誤。

您可能還需要添加以下如果您在IIS7運行+:

<system.webServer> 
    ... 
    <httpErrors errorMode="Detailed" /> 
</system.webServer> 
+0

你在鏈接提供的答案解決問題。謝謝。我認爲剃刀頁面可以在沒有控制器類的情況下工作。 – 1110 2012-01-14 14:46:06