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
Related
I have Swagger setup for an ASP.NET Core 2 MVC API application. I'm using OpenIddict for OAuth but want to customize how the request and responses appear on the documentation.
Since the method in the controller takes an OpenIdConnectRequest, the generated default output looks like so:
... and it just goes on and on.
This is a far cry from the neat JSON required for a client to consume since the middleware does a bunch of work inbetween the client and the controller method.
How do I change how Swagger represents these? I am already using a hack to massage the responses via a custom, private type for token responses, so any help on how to use that would also be appreciated. I have tried to use the SwaggerGenOptions.MapType<> function as the documentation claims that tells Swagger how to map a type to a custom output. Unfortunately, I've not gotten the Swagger output to reflect anything I've done with .MapType<>.
To be clear, these aren't models I control so I can't decorate the members myself.
Note that this is different from How to show WebApi OAuth token endpoint in Swagger. My controller action is discovered fine. Unfortunately, I'm thinking it may be easier to filter it out and use that as another work around to define it manually but I'd rather not if possible.
I'm working on my first Asp.Net Core project, which is to support both a web client, and a Game client (written in Unity).
Now, I started with the default Asp.Net Core Web Application project template provided by Visual Studio 2015, including authentication for Individual User Accounts.
Most, if not all of the generated controllers are returning views, following this pattern in general:
[HttpGet]
[AllowAnonymous]
public IActionResult Get()
{
//Do something smart
return View();
}
This method returns a View as you can see and all is good when requested by the web client.
But what if the Game is sending a request to the same method?
In that case a JSON response is expected (not a View/html document), and my question is what is the recommended "pattern" for supporting this in Asp.Net Core?
Should I place the api returning JSON in separate controllers, hence duplicating some logic? It doesn't feel right to me...
Or should I check the request for an expected response format, and implement logic for returning different responses based on this?
I don't particularly like that approach either...
To me, it seems Asp.Net Core messes things up by providing UI formatted responses directly from the API, but it might be something I've missed here...
Any pointers?
You could move the logic into a class to be called by the controller if you want separate controllers or actions for the Webpage and game.
This would be better and follow the single responsibility rule rather than one action returning two response types
When the return type is IActionResult you can also return Json messages for example as return Json(myReturnObject). You can add an if to return a JSON message and when it is false it will return the view.
You can add an if based on X-Requested-With:
if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return Json(myObject);
}
What I would recommend is to split the requests in different controller methods by response type and add the shared code/logic in a private method or to another class, probably you will use this elsewhere too. By doing this you will be able to do proper tests and won't get confused when it will return the view or a json message.
Hope this helps.
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'm trying to introduce a version rollback within my .net web api application.
Right now I'm using RoutePrefix's on classes to define the version like so:
[RoutePrefix('api/v1')]
public class SomeController {
(SomeController has some api endpoints on it)
Now I've written a custom ControllerSelector, which handles the rollback so that if someone requests api/v2/... it looks to see if v2 exists, and if it doesn't it looks for v1, etc.
I haven't been able to even test this though, because something blocks the routes prior to the call to ControllerSelector. It's like .NET realizes what routes exist prior to calling the ControllerSelector, and decides not to call ControllerSelector because it doesn't think we want to handle the route. It results in a 404.
Is there anyway for me to make routes get passed through properly to the ControllerSelector even if there are no controller classes that use RoutePrefixs and Route attributes to explicitly define those routes?
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.