im new to c# but what im trying to do instead of having a if-statement in every actionresult.
can i have a overall if-statement for the controller and just run that for every actionresult?
public InformationController {
if (Session["CharacterName"] == null)
{
return RedirectToAction("logon", "Auth");
}
something like that?
我将为此实现IRouteConstraint来创建一个类,并以此构建路由。
Create a ActionFilterAttribute
like this:
public class MyFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//your logic here
}
}
and apply this attribute to your controller
[MyFilter]
public class MyController : Controller
This looks like a prime candidate for an Action Filter . Something like this:
public class CheckSessionCharacterNameAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Session["CharacterName"] == null)
{
filterContext.Result = new RedirectToRouteResult(...);
}
}
}
对于ASP.Net MVC框架,我更喜欢实现ActionFilterAttribute类。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.