.net MVC 3 Event Request

I am a little new to .net and trying to understand a few concepts.

I have been writing in Coldfusion for a while, and in CF there is an event under Application.cfc called onRequest () that fires every time there is a page.

What is .net used to capture request information?

And besides, is there a way to block or extend the Request event to disable my own events?

+3
source share
3 answers

You will probably need something like OnActionExecutingthat called before the action is deleted.

To access the current request, you can do the following:

protected virtual void OnActionExecuting(ActionExecutingContext filterContext) {
  //Do the default OnActionExecuting first.
  base.OnActionExecuting(filterContext);

  //The request variable will allow you to see information on the current request.
  var request = filterContext.RequestContext.HttpRequest;
}

, , , .

public class BaseController : Controller
{
  //Code above
}

:

public class HomeController : BaseController
{
}
+4

global.asax HttpApplication (, BeginRequest):

http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx

HttpApplication Request.

, (, css, ).

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_BeginRequest(object sender, EventArgs e)
    {        
        //Request.Have_fun
    }    
}

global.asax, HttpModule.

:

using System;
using System.Web;

namespace MyProject
{
    public class MyHttpModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += ApplicationBeginRequest;
            application.EndRequest += ApplicationEndRequest;
        }

        private void ApplicationEndRequest(object sender, EventArgs e)
        {
            //do something here with HttpContext.Current.Request
        }

        private static void ApplicationBeginRequest(Object source, EventArgs e)
        {
            //do something here with HttpContext.Current.Request
        }


        public void Dispose()
        {
        }
    }
}

web.config( HttpModule):

<system.web>
  <httpModules>
    <add name="MyHttpModule" type="MyProject.MyHttpModule" />
  </httpModules>
</system.web>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="MyHttpModule" type="MyProject.MyHttpModule" />
  </modules>
</system.webserver>

- IIS7 ( system.webServer) web.config.

+6

If you are running ASP.NET MVC 3, I would recommend using global action filters (use one for the β€œevent” you want to handle) instead of directly accessing the ASP.NET application / request stack.

+1
source

Source: https://habr.com/ru/post/1784173/


All Articles