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!
Related
Given a URL, and I want to know which action in which controller is responsible for handling that URL. What I do right now is that I search for the route name in the project from visual studio. But I think there might be a package or tool that lists the routes and their corresponding Controller actions.
Is there a more neat way to find that:
url/examples/1
is handled by:
[HttpGet]
[Route("examples/{id}")]
public Task viewExmaple()
Try Swagger, as I think this will be as close as you can get in terms of "lists the routes and their corresponding Controller actions". By default, swagger acts a GUI (web page) that displays every controller with corresponding controller methods nested beneath(accordion style).
Swagger is also a great tool for debugging and testing. It displays details like which HTTP verb the methods use, which query string params (or JSON payload in body) the method accepts. Great for documentation as well.
Behind the scenes, swagger builds one giant JSON payload that nests all of your controller / controller actions so you may also be able to view it like that.
You would access it by hitting http(s)://your.app.path/swagger
Setting up in a .NET Standard (Non-Core) app
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.
In Startup.cs I want to configure 2 middlewares:
An app.Use (or app.Map with app.Use) which handles websocket requests, but requires a specific dynamic route endpoint.
app.UseMvc() with attribute routing.
How the router can be created and reused in MVC in such situation?
PS. Here is my related question where I am looking at the same problem from a different perspective: Opening a websocket channel inside MVC controller
I had similar issue and after checking the sources I found that the UseMvc method creates its own instance of RouteBuilder and IRouter. This means that you have no control over the route creation while using UseMvc.
So I can suggest creating an alternative to UseMvc that will create two routes mapped to different handlers, one to Mvc and the second to Websocket. Though I haven't tested it yet, hope it helps.
I need to create ASP.NET WebApi with couple operation, for example RegisterAppStart, RegisterStep and RegisterAppEnd. And I want to place all this actions in one controller. As I read, usually in WebApi action name is not using.
And my question - is this a bad idea to rewrite default route config with actions using?
ps. sorry for my English
You can give actions arbitrary names using the [Route("/api/FooController/BarMethod"] attribute routing.
This usually overrides the "REST"yness of your service (where the request method indicates which method should be called), but as you aren't really building a REST service, you shouldn't care.
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.