How does the default .net core controller route "values"? - asp.net-core-2.0

I'm new to .net core and I don't understand how the default generated Controller which has the attribute
[Route("api/[controller]")]
can handle action of "api/values"
how did it translate "[controller]" to handle "values"?

The [controller] token in the route will be replaced by the name of the controller without the controller suffix at runtime.
If you add a new controller
[Route("api/[controller]")]
public class RockController : Controller
{}
Then the [controller] will here be replaced by rock and the route will then be api/rock.
You can read more about token replacements here

Related

Purpose of Error method in asp.net Core 2.0 MVC project template

I'm new to asp.net Core 2.0.
When you Create a new ASP.net Core 2.0 MVC Project (Web Application (Model-View-Controller)), you get a HomeController with Index(), About() and Contact() methods as in MVC 5.
However you also get this:
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
I can't find any documentation in Google or in any books as to what this is for and what the purpose of the single line of code is.
Can somebody please explain it? Is it some kind of best practice - should we be using this?
Thanks.
If time permit, I'll try to update my answer. But for now, the brief explanation is that every public method in your controller is callable as an http endpoint. For instance, the index method in your HomeController renders your index.cshtml view, the About renders the about.cshtml view and so on. In particular the public IActionResult Error() method is called if you redirect the user to the action when an error occurred. Consequently, it returns an Error View containing the information about the error, such as the RequestId and other values bonded with the ErrorViewModel.
I hope this help for now.

Is there a way to remove the "/json/reply/" section of the url?

I would like the URL for a request to be /AmazingRequest (or even /AmazingService) instead of /json/reply/AmazingRequest.
I've tried the Route attribute, but it seems to have no effect. Is it possible within ServiceStack, or would I have to resort to URL rewriting?
This is what I've tried. It compiles, but the attribute has no effect.
public class MyServiceEndpoints : IService
{
[Route("/AmazingService")]
public AmazingResponse Post(AmazingRequest request)
{
return new Amazing(request).GetResponse();
}
}
I realize I would need to tell ServiceStack that it is a json request, but I'm fine with adding the Accept and Content-Type headers or maybe even a ?format=json to the query string.
P.S. I'm using the BSD version of ServiceStack
In ServiceStack Routes are defined on the Request DTO as it's part of your Service Contract, e.g:
[Route("/AmazingService")]
public class AmazingRequest { ... }
The pre-defined Route you're using is because ServiceStack doesn't think there's any custom route defined for your Service and just uses the default one.
The alternative way for declaring your Routes is to use the Fluent Registration API in your AppHost, e.g:
public void Configure(Container container)
{
Routes
.Add<AmazingRequest>("/AmazingService");
}
But the benefit of defining them on the Request DTO's is that your .NET Service Clients will also have access to them and will be able to use your custom routes instead of falling back to the pre-defined routes.

Attribute routing MVC5 route prefix with template

Is it possible to have route prefix with template? I need to specify route/route prefix on my base controller so every controller/action would allow prefixing url with language slug.
www.mydomain.com/en/Search
www.mydomain.com/da/Sogning
www.mydomain.com/Hledat
These three routes would lead to the same action.
I would like something like this
[RoutePrefix("{language?}")]
public abstract class BaseController : Controller
Wouldn't that be better on the Route template?
[Route("{language?}/{action=index}/{id?}")]

How to use Custom Routes with Auto Query

Using the first example in the ServiceStack Auto Query documentation in a project structured similar to the EmailContacts sample project (i.e. separate projects for the ServiceModel and ServiceInterface), how would one register the custom route "/movies" defined by the Route attribute?
[Route("/movies")]
public class FindMovies : QueryBase<Movie>
{
public string[] Ratings { get; set; }
}
Normally, custom routes such as these can be register by passing the ServiceInterface assembly when instantiating AppHostBase:
public AppHost() : base("Email Contact Services", typeof(ContactsServices).Assembly) {}
However, the FindMovies request DTO does not have an associated service and therefore won't be included. No routes are registered.
If I pass typeof(FindMovies).Assembly instead of or in addition to typeof(ContactsServices).Assembly, then the pre-defined route will be registered (i.e. shows up in the metadata, postman, etc.) but the custom route is still not registered (i.e. does not show up in the metadata, postman, etc.).
What is the best way to register the custom route using attributes when there is no service and the ServiceModel and ServiceInterface are in separate projects?
These issues should be resolved in v4.0.24+ that's now available on MyGet.
There's a new AutoQueryFeature.LoadFromAssemblies property to specify an additional list of assemblies to scan for IQuery Request DTO's. This automatically looks in the assemblies where your other Request DTO's are defined so in most cases nothing needs to be done as it will automatically be able to find your query services.
The routes for Query DTO's should now appear on the metadata pages as well as Swagger and Postman metadata API's.

servicestack Razor view pages for response dto's with same name but different namespace

i have 2 dto's in different sub namespaces but the same class name for response dto, in the same assembly.
with the razor view pages in servicestack, it looks for the response dto .cshtml in the View folder.
is it possible to have 2 separate razor view pages for different namespaces?
eg
namespace mydto.Cars
{
public class queryResponse
{
}
}
namespace mydto.Bikes
{
public class queryResponse
{
}
}
Nope, it scans for view pages by name only. I would add the namespace tail to the DTO that describes how the DTO is different, e.g. CarsQueryResponse.
With ServiceStack in general you don't want to have multiple DTOs with the same name but different namespaces, e.g. Every Request DTO must be unique by name, this is what allows you to call a web service knowing the name only.

Resources