namespace Artech.Mvc.ExceptionHandling.Models
{
public class LoginInfo
{
[Display(Name ="User Name")]
[Required(ErrorMessage = "User Name is manadatory!")]
public string UserName { get; set; }
[Display(Name = "Password")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is manadatory!")]
public string Password { get; set; }
}
}
public class AccountController BaseController
{
public AccountController()
base("myPolicy")
{ }
public ActionResult SignIn()
{
return View(new LoginInfo());
}
[HttpPost]
public ActionResult SignIn(LoginInfo loginInfo)
{
if (!ModelState.IsValid)
{
return this.View(new LoginInfo { UserName = loginInfo.UserName });
}
if (loginInfo.UserName != "Foo")
{
throw new InvalidUserNameException();
}
if (loginInfo.Password != "password")
{
throw new UserNamePasswordNotMatchException();
}
ViewBag.Message = "Authentication Succeeds!";
return this.View(new LoginInfo { UserName = loginInfo.UserName });
}
public ActionResult OnSignInError(string userName)
{
return this.View(new LoginInfo { UserName = userName });
}
}
@model Artech.Mvc.ExceptionHandling.Models.LoginInfo
@{
ViewBag.Title = "SignIn";
}
@Html.ValidationSummary()
@if (ViewBag.Messages != null)
{
@ViewBag.Messages
}
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="SignIn" />
}
<exceptionHandling>
<exceptionPolicies>
<add name="myPolicy">
<exceptionTypes>
<add name="InvalidUserNameException"
type="Artech.Mvc.ExceptionHandling.Models.InvalidUserNameException, Artech.Mvc.ExceptionHandling"
postHandlingAction="None">
<exceptionHandlers>
<add name="ErrorMessageSettingHandler"
type="Artech.Mvc.ExceptionHandling.ErrorMessageSettingHandler, Artech.Mvc.ExceptionHandling"
errorMessage="User name does not exist!"/>
</exceptionHandlers>
</add>
<add name="UserNamePasswordNotMatchException"
type="Artech.Mvc.ExceptionHandling.Models.UserNamePasswordNotMatchException, Artech.Mvc.ExceptionHandling"
postHandlingAction="None">
<exceptionHandlers>
<add name="ErrorMessageSettingHandler"
type="Artech.Mvc.ExceptionHandling.ErrorMessageSettingHandler, Artech.Mvc.ExceptionHandling"
errorMessage="User name does not match password!"/>
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<exceptionHandling>
<exceptionPolicies>
<add name="myPolicy">
<exceptionTypes>
<add name="InvalidUserNameException" type="Artech.Mvc.ExceptionHandling.Models.InvalidUserNameException, Artech.Mvc.ExceptionHandling"
postHandlingAction="ThrowNewException">
...
<add name="UserNamePasswordNotMatchException" type="Artech.Mvc.ExceptionHandling.Models.UserNamePasswordNotMatchException, Artech.Mvc.ExceptionHandling"
postHandlingAction="ThrowNewException">
...
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<artech.exceptionHandling>
<add exceptionType="Artech.Mvc.ExceptionHandling.Models.InvalidUserNameException, Artech.Mvc.ExceptionHandling"
errorView="InvalideUserNameError"/>
<add exceptionType="Artech.Mvc.ExceptionHandling.Models.UserNamePasswordNotMatchException, Artech.Mvc.ExceptionHandling"
errorView="UserNamePasswordNotMatchError"/>
</artech.exceptionHandling>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<p style="colorRed; font-weightbold">Sorry,the user name you specify does not exist!</p>
</body>
</html>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<p style="colorRed; font-weightbold">Sorry, The password does not match the given user name!</p>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Artech.Mvc.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
namespace Artech.Mvc.ExceptionHandling
{
public class ExceptionActionInvoker ControllerActionInvoker
{
protected ExceptionHandlingSettings ExceptionHandlingSettings{get; private set;}
protected virtual Func<string, HandleErrorInfo, ViewResult> GetErrorView { get; private set; }
public ExceptionPolicyImpl ExceptionPolicy { get; private set; }
public ExceptionActionInvoker(string exceptionPolicy,Func<string, HandleErrorInfo, ViewResult> getErrorView)
{
this.ExceptionPolicy = EnterpriseLibraryContainer.Current.GetInstance<ExceptionPolicyImpl>(exceptionPolicy);
this.GetErrorView = getErrorView;
this.ExceptionHandlingSettings = ExceptionHandlingSettings.GetSection();
}
public override bool InvokeAction(ControllerContext controllerContext, string handleErrorActionName)
{
ExceptionContext exceptionContext = controllerContext as ExceptionContext;
if (null == exceptionContext)
{
throw new ArgumentException("The controllerContext must be ExceptionContext!", "controllerContext");
}
try
{
exceptionContext.ExceptionHandled = true;
if (this.ExceptionPolicy.HandleException(exceptionContext.Exception))
{
HandleRethrownException(exceptionContext);
}
else
{
if (ExceptionHandlingContext.Current.Errors.Count == 0)
{
ExceptionHandlingContext.Current.Errors.Add(exceptionContext.Exception.Message);
}
ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(exceptionContext);
ActionDescriptor handleErrorAction = FindAction(exceptionContext, controllerDescriptor, handleErrorActionName);
if (null != handleErrorAction)
{
IDictionary<string, object> parameters = GetParameterValues(controllerContext, handleErrorAction);
exceptionContext.Result = this.InvokeActionMethod(exceptionContext, handleErrorAction, parameters);
}
else
{
HandleRethrownException(exceptionContext);
}
}
return true;
}
catch (Exception ex)
{
exceptionContext.Exception = ex;
HandleRethrownException(exceptionContext);
return true;
}
}
protected virtual void HandleRethrownException(ExceptionContext exceptionContext)
{
string errorViewName = this.GetErrorViewName(exceptionContext.Exception.GetType());
string controllerName = (string)exceptionContext.RouteData.GetRequiredString("controller");
string action = (string)exceptionContext.RouteData.GetRequiredString("action");
HandleErrorInfo handleErrorInfo = new HandleErrorInfo(exceptionContext.Exception, controllerName, action);
exceptionContext.Result = this.GetErrorView(errorViewName, handleErrorInfo);
}
protected string GetErrorViewName(Type exceptionType)
{
ExceptionErrorViewElement element = ExceptionHandlingSettings.ExceptionErrorViews
.Cast<ExceptionErrorViewElement>().FirstOrDefault(el=>el.ExceptionType == exceptionType);
if(null != element)
{
return element.ErrorView;
}
if(null== element && null != exceptionType.BaseType!= null)
{
return GetErrorViewName(exceptionType.BaseType);
}
else
{
return "Error";
}
}
}
}
using System;
using System.Web.Mvc;
namespace Artech.Mvc.ExceptionHandling
{
public abstract class BaseController Controller
{
public BaseController(string exceptionPolicy)
{
Func<string, HandleErrorInfo, ViewResult> getErrorView = (viewName, handleErrorInfo) => this.View(viewName, handleErrorInfo);
this.ExceptionActionInvoker = new ExceptionActionInvoker(exceptionPolicy,getErrorView);
}
public BaseController(ExceptionActionInvoker actionInvoker)
{
this.ExceptionActionInvoker = actionInvoker;
}
public virtual ExceptionActionInvoker ExceptionActionInvoker { get; private set; }
protected virtual string GetHandleErrorActionName(string actionName)
{
return string.Format("On{0}Error", actionName);
}
protected override void OnException(ExceptionContext filterContext)
{
using (ExceptionHandlingContextScope contextScope = new ExceptionHandlingContextScope(filterContext))
{
string actionName = RouteData.GetRequiredString("action");
string handleErrorActionName = this.GetHandleErrorActionName(actionName);
this.ExceptionActionInvoker.InvokeAction(filterContext, handleErrorActionName);
foreach (var error in ExceptionHandlingContext.Current.Errors)
{
ModelState.AddModelError(Guid.NewGuid().ToString() ,error.ErrorMessage);
}
}
}
}
}
public class ExceptionHandlingContext
{
[ThreadStatic]
private static ExceptionHandlingContext current;
public ExceptionContext ExceptionContext { get; private set; }
public ModelErrorCollection Errors { get; private set; }
public ExceptionHandlingContext(ExceptionContext exceptionContext)
{
this.ExceptionContext = exceptionContext;
this.Errors = new ModelErrorCollection();
}
public static ExceptionHandlingContext Current
{
get { return current; }
set { current = value; }
}
}
[ConfigurationElementType(typeof(ErrorMessageSettingHandlerData))]
public class ErrorMessageSettingHandler IExceptionHandler
{
public string ErrorMessage { get; private set; }
public ErrorMessageSettingHandler(string errorMessage)
{
thisErrorMessage = errorMessage;
}
public Exception HandleException(Exception exception, Guid handlingInstanceId)
{
if (null == ExceptionHandlingContextCurrent)
{
throw new InvalidOperationException("");
}
if (stringIsNullOrEmpty(thisErrorMessage))
{
ExceptionHandlingContextCurrentErrorsAdd(exceptionMessage);
}
else
{
ExceptionHandlingContextCurrentErrorsAdd(thisErrorMessage);
}
return exception;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有