What is the difference between ActionFilters and HTTPHandlers? In the context of a MVC web app, to authenticate a user. How would I use either of them to perform authentication?
Action filter
An action filter is an attribute. You can apply most action filters to either an individual controller action or an entire controller.
HTTP Handlers
HTTP Handlers are any Class that implements System.Web.IHttpHandler Interface becomes HttpHandler . And this class run as processes in response to a request made to the ASP.NET Site. The most common handler is an ASP.NET page handler that processes .aspx files.
Related
I need to delete a specific cookie when my app starts, before heading to home page.
I had this inside a controller action method, with a redirect to home page, setting up my startup class to use as route template this controller and action method.
However, there must be a way I can set up a method to delete this cookie, and execute it from startup?
In ASP.NET, this would be done in the methods of global.asax (often in Session_Start(...)). Read more here and here.
In ASP.NET Core, the startup.cs class is where all configurations of services are defined, as well as pipeline requests are managed.
You need to make your own custom middleware for this. Middleware is software that's assembled into an app pipeline to handle requests and responses.
There is another SO question on this topic here (with an answer):
ASP .NET Core webapi set cookie in middleware
For more in-depth cookie management look at this article:
https://www.seeleycoder.com/blog/cookie-management-asp-net-core/
More on middleware:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-2.2
I'm trying to understand and learn the architecture and pipeline of ASP.NET. So far I understand the architectural overview:
on how we get from client to the IIS webserver (request)
via ISAPI extension to the ASP.NET runtime
from there on into the HTTP Pipeline
and ASP.NET calling the HttpModules and HttpHandler
in case of using MVC, selecting the MvcHandler
which is returned by the MvcRouteHandler
etc etc
Now what I don't understand (or can't find any resource on the web documentating this part), is how does the asp.net runtime detect which HttpHandler it has to select for it's request? So how does it know based on my Visual Studio solution that it's an MVC application for example? How does it figure it out that it should use the MvcHttpHandlers? Is there an assembly scan with Reflection somewhere in the HTTP pipeline? Because it certainly isn't a configuration telling the runtime to use the MvcHandler, or is it?
So basically at what exact point is the HttpContext.CurrentHandler being set?
Application_Start
When the request arrives to IIS and the endpoint corresponds to an
Asp.Net application, then the first event raised is the
Application_Start in the System.Web.HttpApplication object.
RouteTable.Routes.Add
Into this event of an Mvc app you can set the routing rules that do
match endpoint urls with Controllers and Actions methods
in the application and the relative IRouteHandler object type,
that will be typeof(MvcRouteHandler).
(see Scott Guthrie post)
HttpApplication.MapRequestHandler
Therefore, soon after that, when the routing table has been setted up,
in the subsequents events (or better in the methods that compose the
pipeline orchestrated by the Asp.Net Framework under IIS control
(integrated pipeline)) of the Asp.Net http request management,
when it needs to know how to manage the http request itself
(HttpApplication.MapRequestHandler), it get parsed the url in
the HttpContext object against the rules in the routing table,
and when it get found a matching, it is instatiated the right type of
its handler, MvcRouteHandler in our case, which will return the
IHttpHandler object by the method GetHttpHandler(RequestContext): MvcHandler .
(see Msdn MvcRoutHandler.GetHttpHandler)
MvcHandler.ProcessRequest
MvcHandler in turn, will give start to the real MVC request handling
through the Asp.Net pipeline event ProcessRequest: and so will
be instatiated the right Controller through the
ControllerFactory, and will be called the Execute method of the Controller abstract base class of the instatiated
controller object, and finally the right Action through the
ActionInvoker.
(see Msdn MvcHandler.ProcessRequest, and The Asp.Net Mvc pipeline)
final note on RouteCollection.MapRoute The Visual Studio starter template for MVC projects creates project that use MapRoute extension method
instead of RouteTable.Routes.Add. This method is very useful
because avoids to have to use always typeof(MvcRouteHandler)
expression when adding new url routing to the project.
(see Asp.Net Routing and Asp.Net Mvc)
In this Adding a Controller (C#) tutorial I read:
ASP.NET MVC invokes different controller classes (and different action
methods within them) depending on the incoming URL
However, I am looking now at the source code full of such Controllers, the web app is fully functional, switches from Controller to Controller smoothly, without ever changing the URL...
How does this work?
Also, based on method, so one action might handle POST requests because it is decorated with [HttpPost] attribute and another one for GET requests as it is decorated with [HttpGet] attribute
I need to do some more guesswork as you've not detailed your observation: your application might send AJAX requests, and while the Url of the page is not changing, different actions in controllers are invoked because of these behind the scene requests.
I have a few controllers and a few methods decorated with a [Route] attribute.
Now the thing is, i want to add a global message handler excluding one single controller.
here is a snapshot from the http configuration code:+
The problem is, my Free controller (defined in the FreeApi route) is still getting the Custom message handler somehow in the pipeline.
How can i achieve this kind of behavior?
Thanks!
I would like to redirect to an action controller gracefully after session expires. Added Session_OnEnd method in my Global.asax file. See code below:
Global.asax:
public void Session_OnEnd()
{
//redirect to controller action here
}
At first I had "Response.RedirectToRoute" inside the method but throws an exception and can't work technically.
You can't do this. Session_End could be fired without an actual HTTP context. The user might have even closed his browser long before this event gets fired so there is nowhere to redirect to. The Request and Response objects are not available.
But you can create custom ActionFilter for handling this issue.
Redirect at Session Timeout in Global.asax in mvc4
Detecting Session expiry on ASP.NET MVC
Detecting Session Timeouts using a ASP.Net MVC Action Filter