本文整理汇总了C#中System.Web.Mvc.ViewResult.ExecuteResult方法的典型用法代码示例。如果您正苦于以下问题:C# ViewResult.ExecuteResult方法的具体用法?C# ViewResult.ExecuteResult怎么用?C# ViewResult.ExecuteResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Mvc.ViewResult
的用法示例。
在下文中一共展示了ViewResult.ExecuteResult方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Application_Error
//Обработка ошибок для всего приложения
protected void Application_Error(object sender, EventArgs e)
{
string requestType=Request.RequestType;
if (requestType.Equals("GET"))
{
HttpContext ctx = HttpContext.Current;
Exception ex = ctx.Server.GetLastError();
var httpException = ex as HttpException;
if (httpException != null)
{
if (httpException.GetHttpCode() == 404)
{
ctx.Response.Clear();
RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
IController controller = new HomeController(); // Здесь можно использовать любой контроллер
var context = new ControllerContext(rc, (ControllerBase)controller);
var viewResult = new ViewResult();
//страницы будут искаться в Views/Shared
viewResult.ViewName = "Error404";
viewResult.ViewData.Model = new HandleErrorInfo(ex, context.RouteData.GetRequiredString("controller"), context.RouteData.GetRequiredString("action"));
viewResult.ExecuteResult(context);
ctx.Server.ClearError();
}
}
}
}
示例2: Application_Error
protected void Application_Error()
{
try
{
var logger = ObjectFactory.GetInstance<ILog>();
Exception exception = Server.GetLastError() ?? new Exception("Unable to get exception.");
logger.Fatal("An unhandled exception occurred.", exception);
}
catch
{ }
HttpContext ctx = HttpContext.Current;
ctx.Response.Clear();
RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
string controllerName = rc.RouteData.GetRequiredString("controller");
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(rc, controllerName);
var cc = new ControllerContext(rc, (ControllerBase)controller);
var viewResult = new ViewResult { ViewName = "Error" };
viewResult.ExecuteResult(cc);
ctx.Server.ClearError();
}
示例3: RedirectToError
private static void RedirectToError()
{
HttpContext ctx = HttpContext.Current;
var errorMessage = Convert.ToString(ctx.Server.GetLastError());
var error = new KeyValuePair<string, object>("ErrorMessage", errorMessage);
foreach (var er in ctx.AllErrors)
{
InternalLog.WriteLog(HttpContext.Current.Request.UserAgent,
HttpContext.Current.Request.Url.ToString(),
er.Message,
er.StackTrace.ToString());
}
ctx.Response.Clear();
if (ctx.CurrentHandler != null && ctx.CurrentHandler.GetType().ToString().Equals("System.Web.Mvc.MvcHandler"))
{
RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(rc, "Home");
var cc = new ControllerContext(rc, (ControllerBase)controller);
var viewResult = new ViewResult { ViewName = "InternalError" };
viewResult.ViewData.Add(error);
viewResult.ExecuteResult(cc);
ctx.Response.End();
}
}
示例4: ExecuteResult
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var response = context.HttpContext.Response;
var request = context.HttpContext.Request;
var requestedUrl = GetRequestedUrl(request);
NotFoundConfig.OnNotFound(context.RequestContext.HttpContext.Request, new Uri(requestedUrl));
this.ViewData["RequestedUrl"] = requestedUrl;
this.ViewData["ReferrerUrl"] = GetReferrerUrl(request, request.Url.OriginalString);
// Make sure the status code is 404 for the search engine bots
response.StatusCode = 404;
// Prevent IIS7 from overwriting our error page!
response.TrySkipIisCustomErrors = true;
var viewResult = new ViewResult
{
ViewName = this.ViewName,
ViewData = this.ViewData
};
response.Clear();
viewResult.ExecuteResult(context);
}
示例5: Execute
public void Execute(GenericResult genericResult, ControllerContext controllerContext)
{
if (genericResult.Refresh)
{
new RedirectResult(controllerContext.HttpContext.Request.RawUrl).ExecuteResult(controllerContext);
return;
}
if (genericResult.RedirectUri != null)
{
new RedirectResult(genericResult.RedirectUri.ToString()).ExecuteResult(controllerContext);
return;
}
var viewName = genericResult.ResultName ?? (string)controllerContext.RouteData.Values["action"];
ViewEngineResult view = ViewEngines.Engines.FindView(controllerContext, viewName, null);
if (view.View == null)
return;
var viewResult = new ViewResult();
controllerContext.Controller.ViewData.Model = genericResult.Model;
viewResult.ViewName = viewName;
viewResult.ViewData = controllerContext.Controller.ViewData;
viewResult.TempData = controllerContext.Controller.TempData;
foreach (var message in genericResult.Messages.Where(m => m.Category == MessageCategory.Error))
controllerContext.Controller.ViewData.ModelState.AddModelError(message.Field ?? message.Text, message.Text);
viewResult.ExecuteResult(controllerContext);
}
示例6: ExecuteResult
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
var request = context.HttpContext.Request;
// TODO: Add logic for not authorized requests
string viewName = StatusCode == HttpStatusCode.NotFound ? NotFoundView : ErrorView;
// We need to clear both headers and content
// because Clear() doesn't clear the headers
// see: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.clear.aspx
response.ClearHeaders();
response.ClearContent();
response.StatusCode = (int)StatusCode;
// Certain versions of IIS will sometimes use their own error page when
// they detect a server error. Setting this property indicates that we
// want it to try to render ASP.NET MVC's error page instead.
response.TrySkipIisCustomErrors = true;
if (request.IsAjaxRequest())
{
// TODO: Add logic for AJAX requests
}
var viewResult = new ViewResult
{
ViewName = viewName,
ViewData = ViewData ?? new ViewDataDictionary()
};
viewResult.ExecuteResult(context);
}
示例7: ExecuteResult
private void ExecuteResult(ControllerBase controller)
{
var result = new ViewResult
{
ViewData = controller.ViewData,
TempData = controller.TempData
};
result.ExecuteResult(controller.ControllerContext);
}
示例8: Execute
public override void Execute(object model, ControllerContext context)
{
context.HttpContext = new HttpContextWrapper(HttpContext.Current);
var viewResult = new ViewResult {TempData = context.Controller.TempData, ViewData = context.Controller.ViewData};
viewResult.ViewData.Model = null;
viewResult.ExecuteResult(context);
}
示例9: View
private new ActionResult View(string viewFolder, string viewName, object model = null)
{
var view = new ViewResult();
view.TempData = TempData;
view.ViewName = (viewFolder != null ? ("~/Views/" + viewFolder + "/" + viewName + ".cshtml") : viewName);
view.ViewData = ViewData;
view.ViewData.Model = model;
view.ExecuteResult(ControllerContext);
return null;
}
示例10: Application_Error
protected void Application_Error()
{
ErrorLoger.LogEventError(Server.GetLastError());
ErrorLoger.LogEventError(Context.Error);
Response.Clear();
Server.ClearError();
var res = new ViewResult {ViewName = "Error"};
res.ExecuteResult(new ControllerContext(Request.RequestContext, (ControllerBase) new HomeController()));
}
示例11: Application_Error
protected void Application_Error(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
Exception ex = ctx.Server.GetLastError();
ctx.Response.Clear();
RequestContext rc = ((MvcHandler) ctx.CurrentHandler).RequestContext;
IController controller = new ErrorController();
var context = new ControllerContext(rc, (ControllerBase) controller);
var viewResult = new ViewResult();
if (ex is HttpRequestValidationException)
viewResult.ViewName = "HttpRequestValidationException";
else
{
var httpException = ex as HttpException;
if (httpException != null)
{
switch (httpException.GetHttpCode())
{
case 404:
viewResult.ViewName = "Error404";
break;
case 500:
viewResult.ViewName = "Error500";
break;
default:
viewResult.ViewName = "Error";
break;
}
}
else
{
viewResult.ViewName = "Error";
}
}
context.RouteData.Values["controller"] = "Error";
context.RouteData.Values["action"] = viewResult.ViewName;
context.RouteData.Values["exception"] = ex;
viewResult.ViewData.Model = new HandleErrorInfo(ex, context.RouteData.GetRequiredString("controller"),
context.RouteData.GetRequiredString("action"));
log.Error(ex.Message);
viewResult.ExecuteResult(context);
ctx.Server.ClearError();
}
示例12: Application_Error
protected void Application_Error(object sender, EventArgs e)
{
HttpContext httpContext = HttpContext.Current;
// Для администратора отображаем стандартный вывод ошибки
if (httpContext.User != null &&
httpContext.User.Identity != null &&
httpContext.User.Identity.IsAuthenticated)
{
return;
}
Exception ex = httpContext.Server.GetLastError();
httpContext.Response.Clear();
if (new HttpRequestWrapper(Request).IsAjaxRequest())
{
Response.StatusCode = 500;
Response.ContentType = "application/json";
Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new
{
errorMessage = ex.Message
}));
return;
}
RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext;
IController controller = new HomeController();
var context = new ControllerContext(requestContext, (ControllerBase)controller);
ViewResult viewResult = new ViewResult();
HttpException httpException = ex as HttpException;
if (httpException != null)
{
viewResult.ViewBag.HttpCode = httpException.GetHttpCode();
// Исправляем ошибку ELMAH (Request Validation errors does not get logged)
// https://code.google.com/p/elmah/issues/detail?id=217
if (httpException is HttpRequestValidationException)
{
Elmah.ErrorLog.GetDefault(httpContext).Log(new Error(httpException));
}
}
viewResult.ViewName = "Error";
viewResult.ViewData.Model = new HandleErrorInfo(ex, context.RouteData.GetRequiredString("controller"), context.RouteData.GetRequiredString("action"));
viewResult.ExecuteResult(context);
httpContext.ClearError();
}
示例13: Application_Error
protected void Application_Error(object sender, EventArgs e)
{
try
{
HttpContext ctx = HttpContext.Current;
Exception ex = ctx.Server.GetLastError();
ctx.Response.Clear();
RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
IController controller = new HomeController();
// Тут можно использовать любой контроллер, например тот что используется в качестве базового типа
var context = new ControllerContext(rc, (ControllerBase)controller);
var viewResult = new ViewResult();
var httpException = ex as HttpException;
if (httpException != null)
{
switch (httpException.GetHttpCode())
{
case 404:
viewResult.ViewName = "Error404";
break;
case 401:
viewResult.ViewName = "Error401";
break;
default:
viewResult.ViewName = "Error";
break;
}
}
else
{
viewResult.ViewName = "Error";
}
viewResult.ViewData.Model = new HandleErrorInfo(ex, context.RouteData.GetRequiredString("controller"),
context.RouteData.GetRequiredString("action"));
viewResult.ExecuteResult(context);
ctx.Server.ClearError();
}
catch (Exception)
{
var viewResult = new ViewResult();
viewResult.ViewName = "Error";
//throw;
}
}
示例14: HandleUnknownAction
protected override void HandleUnknownAction(string actionName)
{
var controllerName = (string)RouteData.Values["controller"];
var ex = Server.GetLastError();
var model = new HttpErrorViewModel(ex ?? new HttpException(500, "Something went wrong."), controllerName, actionName);
var result = new ViewResult
{
ViewName = "~/Views/Error/Index.cshtml",
ViewData = new ViewDataDictionary<HttpErrorViewModel>(model),
};
Response.StatusCode = model.StatusCode;
result.ExecuteResult(ControllerContext);
}
示例15: OnException
public void OnException(ExceptionContext filterContext)
{
HttpContext ctx = HttpContext.Current;
Exception ex = ctx.Server.GetLastError();
ctx.Response.Clear();
RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
IController controller = new HomeController(); // Здесь можно использовать любой контроллер
var context = new ControllerContext(rc, (ControllerBase)controller);
var viewResult = new ViewResult();
var httpException = ex as HttpException;
if (httpException != null)
{
switch (httpException.GetHttpCode())
{
//страницы будут искаться в Views/Shared
case 404:
viewResult.ViewName = "Error404";
break;
case 500:
viewResult.ViewName = "Error500";
break;
default:
viewResult.ViewName = "Error";
break;
}
}
else
{
viewResult.ViewName = "Error";
}
viewResult.ViewBag.Message = filterContext.Exception.Message;
//logging
HttpSessionStateBase sessionBase = new HttpSessionStateWrapper(ctx.Session);
AppLogger.Error(sessionBase, filterContext.Exception.Message);
try{
viewResult.ViewBag.TargetSite = filterContext.Exception.TargetSite;
viewResult.ViewBag.StackTrace = filterContext.Exception.StackTrace;
}catch(Exception e){}
viewResult.ExecuteResult(context);
ctx.Server.ClearError();
}