• MVC拦截


    1using System;
     2using System.Collections.Generic;
     3using System.Linq;
     4using System.Web;
     5using System.Web.Mvc;
     6using System.Web.Mvc.Ajax;
     8namespace MVCDemo.Controllers.Filters
     9{
       public class LoggerFilter : FilterAttribute, IActionFilter
       {
           void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
           {
               filterContext.Controller.ViewData["ExecutingLogger"] = "正要添加公告,已以写入日志!时间:" + DateTime.Now; 
           }
    17        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
           {
               filterContext.Controller.ViewData["ExecutedLogger"] = "公告添加完成,已以写入日志!时间:" + DateTime.Now;
           }
       }
    22}
    

      

    复制代码 1using System;
     2using System.Collections.Generic;
     3using System.Linq;
     4using System.Web;
     5using System.Web.Mvc;
     6using System.Web.Mvc.Ajax;
     7
     8namespace MVCDemo.Controllers.Filters
     9{
    10    public class ExceptionFilter : FilterAttribute,IExceptionFilter
    11    {
    12        void IExceptionFilter.OnException(ExceptionContext filterContext)
    13        {
    14            filterContext.Controller.ViewData["ErrorMessage"] = filterContext.Exception.Message;
    15            filterContext.Result = new ViewResult()
    16            {
    17                ViewName = "Error",
    18                ViewData = filterContext.Controller.ViewData,
    19            };
    20            filterContext.ExceptionHandled = true;
    21        }
    22    }
    23}
    

      

    1using System;
     2using System.Collections.Generic;
     3using System.Linq;
     4using System.Web;
     5using System.Web.Mvc;
     6using System.Web.Mvc.Ajax;
     7using MVCDemo.Models;
     8using MVCDemo.Models.Interfaces;
     9using MVCDemo.Models.Entities;
    10using MVCDemo.Controllers.Filters;
    12namespace MVCDemo.Controllers
    13{
       public class AnnounceController : Controller
       {
           public ActionResult Release()
           {
               ICategoryService cServ = ServiceBuilder.BuildCategoryService();
               List<CategoryInfo> categories = cServ.GetAll();
               ViewData["Categories"] = new SelectList(categories, "ID", "Name");
               return View("Release");
           }
    24        [LoggerFilter()]
           [ExceptionFilter()]
           public ActionResult DoRelease()
           {
               AnnounceInfo announce = new AnnounceInfo()
               {
                   ID = 1,
                   Title = Request.Form["Title"],
                   Category = Int32.Parse(Request.Form["Category"]),
                   Content = Request.Form["Content"],
               };
    36            IAnnounceService aServ = ServiceBuilder.BuildAnnounceService();
               aServ.Release(announce);
    39            ViewData["Announce"] = announce;
    41            System.Threading.Thread.Sleep(2000);
               ViewData["Time"] = DateTime.Now;
               System.Threading.Thread.Sleep(2000);
    45            return View("ReleaseSucceed");
           }
       }
    48}
    

      -----------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace WebApplication1.App_Start
    {
        public class ControllerValidateAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext actionContext)
            {
                var conLoginName = ConfigurationManager.AppSettings["LoginName"].ToString();
                try
                {
                    string loginName = actionContext.HttpContext.Request.Url.AbsolutePath.ToLower().TrimEnd('/').ToString();
                    if (loginName != conLoginName)
                    {
                        var conKey = ConfigurationManager.AppSettings["InfoAPIKey"].ToString();
                        var token = actionContext.HttpContext.Request.Headers.Get("token").First().ToString();
                        if (token == conKey)
                        {
                            //actionContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { Controller = "", action = "" }));
                        }
                        else
                        {
                            actionContext.Result = new ContentResult { Content = @"The illegal user access" };
                        }
                    }
                }
                catch (Exception ex)
                {
                    actionContext.HttpContext.Response.Write(ex);
                }
            }
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;
    
    namespace WebApplication1.App_Start
    {
        public class ApiControllerValidateAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(HttpActionContext actionContext)
            {
                var conLoginName = ConfigurationManager.AppSettings["LoginName"].ToString();
                try
                {
                    string loginName = actionContext.Request.RequestUri.AbsolutePath.ToLower().TrimEnd('/').ToString();
                    if (loginName != conLoginName)
                    {
                        var conKey = ConfigurationManager.AppSettings["InfoAPIKey"].ToString();
                        var token = actionContext.Request.Headers.GetValues("token").First().ToString();
                        if (token == conKey)
                        {
                            //成功
                        }
                        else 
                        {
                            //失败
                            actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Forbidden);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
                base.OnActionExecuting(actionContext);
            }
        }
    }
    

      需要调用的 Controller类上加上验证类名

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using WebApplication1.App_Start;

    namespace WebApplication1.Controllers
    {
        [ControllerValidate]
        public class Test1Controller : Controller
        {
            //
            // GET: /Test1/
            public ActionResult Index()
            {
                return View();
            }
        }
    }

    using WebApplication1.App_Start;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    namespace MvcApplication1.Controllers
    {
        [ApiControllerValidate]
        public class TestController : ApiController
        {
            [HttpGet]
            public dynamic Index()
            {
                return Request.CreateResponse(HttpStatusCode.OK, new
                {
                    Success = true,
                    Message = ""
                });
            }
        }
    }

  • 相关阅读:
    BZOJ2298: [HAOI2011]problem a
    BZOJ4066: 简单题
    BZOJ2131: 免费的馅饼
    Educational Codeforces Round 97 div2
    [SCOI2016]背单词
    [SCOI2015]情报传递(离线树状数组跑图)
    树上主席树(无代码,单纯谈思路的一篇水文)
    CF Round #679 div2赛后总结
    [SCOI2015]小凸解密码(平衡树、线段树做法)
    CF Round #677 div3 赛后总结
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7755282.html
Copyright © 2020-2023  润新知