C# 如何使用包含资源值的IController Execute()返回视图?
我正在做自定义错误处理C# 如何使用包含资源值的IController Execute()返回视图?,c#,asp.net,asp.net-mvc,error-handling,resources,C#,Asp.net,Asp.net Mvc,Error Handling,Resources,我正在做自定义错误处理 我的代码: Global.asax: public void Application_Error(Object sender, EventArgs e) { Exception exception = Server.GetLastError(); Server.ClearError(); var routeData = new RouteData(); routeData.Values.Add("
我的代码:
Global.asax:
public void Application_Error(Object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Server.ClearError();
var routeData = new RouteData();
routeData.Values.Add("controller", "ErrorPage");
routeData.Values.Add("action", "Error");
routeData.Values.Add("exception", new HandleErrorInfo(exception, "ErrorPage", "Error"));
if (exception.GetType() == typeof(HttpException))
{
routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
}
else
{
routeData.Values.Add("statusCode", 500);
}
Response.TrySkipIisCustomErrors = true;
var controller = new ErrorPageController();
((IController)controller).Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
Response.End();
}
控制器:
public class ErrorPageController : Controller
{
public ActionResult Error(int statusCode, HandleErrorInfo exception)
{
Response.StatusCode = statusCode;
ViewBag.StatusCode = statusCode.ToString();
return View(exception);
}
}
视图:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title=Resource.Error++(!String.IsNullOrEmpty(ViewBag.StatusCode))?ViewBag.StatusCode:“500”;
}
@(Resource.Error++(!String.IsNullOrEmpty(ViewBag.StatusCode))?ViewBag.StatusCode:“500”+“+Resource.Sorry):
这是一个基本上略有变化的答案。如果我的视图包含任何
资源值,则浏览器只会获取错误代码,而不会获取页面。如果我把它们全部去掉,效果就好了。有什么想法可以让视图与资源一起工作吗?
此外,是否应在方法中处理来自应用程序的控制器错误?如果文件未完全满,有时资源不会出现。例如,如果资源文件有20个条目,则resource.lang.resx必须有相同的20个条目。您当时不使用它并不重要如果文件未完全满,有时资源不会出现。例如,如果resource.rsx文件有20个条目,则resource.lang.resx必须有相同的20个条目。你不使用它也没关系time@DaniilT. 知道了。以后可能会尝试完全填充文件。在这种情况下,资源值是否可能不只对一个视图有效?我给出了答案。可能我不太确定。但我有一种情况,当资源在所有资源文件中都未满时,本地化并没有出现。
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = Resource.Error+ " " + (!String.IsNullOrEmpty(ViewBag.StatusCode)) ? ViewBag.StatusCode : "500" ;
}
<h1 class="error">@(Resource.Error + " " + (!String.IsNullOrEmpty(ViewBag.StatusCode)) ? ViewBag.StatusCode : "500" +". "+ Resource.Sorry):</h1>
<h4><a href ="@Url.Action("Index","Home")"><i>@Resource.MainPage</i></a></h4>