Using dash in the URL query in ASP.NET Core - asp.net-core

Can we use dashes (-) in the Route template in ASP.NET Core?
// GET: api/customers/5/orders?active-orders=true
[Route("customers/{customer-id}/orders")]
public IActionResult GetCustomerOrders(int customerId, bool activeOrders)
{
.
.
.
}
(The above code doesn't work)

The route parameters usually directly map to the action's variable name, so [Route("customers/{customerId}/orders")] should work since that's the name of your variable (int customerId).
You don't need dashes there, the part within the curly braces {} will never appear as a part of the generated url, it will always be replaced by the content you pass from browser or the variables you pass to the url generator.
customers/{customerId}/orders will always be customers/1/orders when customerId is set to 1, so there's no point trying to force it to {customer-id}.
However, you can try public
[Route("customers/{customer-id}/orders")]
IActionResult GetCustomerOrders([FromRoute(Name = "customer-id")]int customerId, bool activeOrders)
to bind the customerId from a unconventional route name, if you wish. But I'd strongly advise against it, as it just adds unnecessary code which has absolutely zero-effect on your generated urls.
The above generates (and parses) the exactly same url as
[Route("customers/{customerId}/orders")]
IActionResult GetCustomerOrders(int customerId, bool activeOrders)
and is much more readable code.
For the query part, as you figured it out in the comments, it makes sense to add the dashes via [FromQuery(Name = "active-orders")] bool activeOrders, since that really affects the generated url.
New in ASP.NET Core 2.2
In ASP.NET Core 2.2 you'll get a new option to 'slugify' your routes (only supported when using the new Route Dispatcher instead of the default Mvc Router).
A route of blog\{article:slugify} will (when used with Url.Action(new { article = "MyTestArticle" })) generate blog\my-test-article as url.
Can also be used in default routes:
routes.MapRoute(
name: "default",
template: "{controller=Home:slugify}/{action=Index:slugify}/{id?}");
For further details see the ASP.NET Core 2.2-preview 3 annoucement.

Just expanding on Tseng answer to the question. for ASP NET CORE to use "slugify" transformer you need to register it first like so:
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null) { return null; }
return Regex.Replace(value.ToString(),
"([a-z])([A-Z])",
"$1-$2",
RegexOptions.CultureInvariant,
TimeSpan.FromMilliseconds(100)).ToLowerInvariant();
}
}
and then in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddRouting(options =>
{
options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});
}
Code from Microsoft

Related

Migrate IRouter usage to ASP.NET Core 3.1

I'm reading through Adam Freeman's Pro ASP.NET Core MVC 2 and one of the chapters about advanced routing features includes a mechanism whereby you can implement two-way legacy URL handling via IRouter. The gist of it is this:
Suppose you have a "legacy" URL like "/article/Windows_3.1_Overview.html"
Using a custom IRouter implementation, Core 2.0 lets you:
Direct that legacy URL to a specific action (e.g. Legacy/GetLegacyUrl) while passing in the URL as a parameter as so:
public async Task RouteAsync(RouteContext context)
{
string requestedUrl = context.HttpContext.Request.Path.Value.TrimEnd('/');
if (urls.Contains(requestedUrl, StringComparer.OrdinalIgnoreCase))
{
context.RouteData.Values["controller"] = "Legacy";
context.RouteData.Values["action"] = "GetLegacyUrl";
context.RouteData.Values["legacyUrl"] = requestedUrl;
await mvcRoute.RouteAsync(context); // mvcRoute is an instance of MvcRouteHandler
}
}
Generate that same URL using a tag helper: (<a asp-route-legacyurl="/article/Windows_3.1_Overview.html">Old Link</a>) using the following:
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
if (context.Values.ContainsKey("legacyUrl"))
{
string url = context.Values["legacyUrl"] as string;
if (urls.Contains(url))
{
return new VirtualPathData(this, url);
}
}
return null;
}
My question is: how do I do that in Core 3.0? I've tried this approach but there is no MvcRouteHandler anymore. I've tried implementing DynamicRouteValueTransformer like so:
public async override ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext,
RouteValueDictionary values)
{
string requestedUrl = httpContext.Request.Path.Value.TrimEnd('/');
return await Task.FromResult(new RouteValueDictionary()
{
["controller"] = "Legacy",
["action"] = "GetLegacyUrl",
["legacyUrl"] = requestedUrl
});
}
... but as far as I've read, this only works one way. It's also the only thing mentioned in Microsoft's 2.2 -> 3.0 migration guide. I've tried to just literally map the URL using
routes.MapRoute(
name: "",
template: route,
defaults: new { controller = "Legacy", action = "GetLegacyUrl", legacyUrl = route });
But this also doesn't generate the legacy URL, instead opting for Legacy/GetLegacyUrl/?legacyUrl=%2Farticle%2FWindows_3.1_Overview.html
I'm not really sure how else I can achieve this and I've been racking my brain and the documentation for several hours now. "Routing in ASP.NET Core" didn't help, neither did "Migrate from ASP.NET Core 2.2 to 3.0".
I'm probably missing something obvious, but I just can't seem to find an answer.
You could get the default mvc route handler using routes.DefaultHandler
In LegacyRoute.cs file, change your constructor signature from
public LegacyRoute(IServiceProvider services, params string[] targetUrls)
To
public LegacyRoute(IRouter routeHandler, params string[] targetUrls)
In Startup.cs file, add the route like this given below
routes.Routes.Add(new LegacyRoute(routes.DefaultHandler, "/articles/Windows_3.1_Overview.html", "/old/.NET_1.0_Class_Library"));

Netcore 2.2 Localized Routing - Route values for default culture always ignored

Successfully using the project laid out at Localized routing using ASP.NET Core MVC 2. The project is developed to display the Default language using just the controller/Action and Localized Routes for the alternate languages.
My Controller
[LocalizationRoute("en", "Portal/Dashboard")]
[LocalizationRoute("fr", "Portail/tableau-de-bord")]
[LocalizationRoute("es", "Portal/Tablero")]
public class DashboardController : PortalBaseController
{
private const string Title = "Dashboard";
[LocalizationRoute("en", "Dashboardv1")]
[LocalizationRoute("fr", "tableau-de-bordv1")]
[LocalizationRoute("es", "Tablerov1")]
public IActionResult Dashboardver1()
In my SignIn controller I wish to create a localised RedirectUrl...the below code provides a fully localized route for the the french and spanish but the default route is always just controller/action. If I manually type in the english, fully localized route, the page loads but the code below always returns just a regular path.
var culture = CultureInfo.CurrentCulture.Name;
var redirectUrl = LocalizationRouteDataHandler.GetUrl("Dashboard", "Dashboardver1", culture).Url;
//should be "en/portal/Dashboard/Dashboardv1"
//currently resolves to "Dashboard/Dashboardver1"
Suggestions as ?
I find that In LocalizationRouteDataHandler.AddControllerRouteData ,code ControllerRoutes[controllerKey].Names.TryAdd(culture, route); adds culture="en" and route="Dashboard" first to the Dictionary of Controller Name and then when it tries to add the condition culture="en" and route="en/Portal/Dashboard",it fails since the "en" key has added.
A workaround is that you could remove the unnecessary key and add the new key.
LocalizationRouteDataHandler.cs
public static void AddControllerRouteData(string controller, string culture, string route)
{
if (controller == "Dashboard")
{
Console.WriteLine("test");
}
string controllerKey = controller.ToLower();
// If the controller doesn't exist, create it!
if (!ControllerRoutes.ContainsKey(controllerKey))
{
ControllerRoutes.TryAdd(controllerKey, new CultureControllerRouteData());
}
// key removed
if (culture=="en" && ControllerRoutes[controllerKey].Names.Remove("en"))
{
ControllerRoutes[controllerKey].Names.TryAdd(culture, route);
}
// dictionary doesn't contain the key
else
{
ControllerRoutes[controllerKey].Names.TryAdd(culture, route);
}
}
When I test with
var redirectUrl = LocalizationRouteDataHandler.GetUrl("Dashboard", "Dashboardver1", "en").Url;
It returns /en/Portal/Dashboard/Dashboardver1

ASP.NET Core Identity as UI base URL

Identity URLs are all of the form : /Identity/Account/Login etc
How can I change them (all) to be of the form /myapp/Identity/Account/Login etc ?
Is there a single "base" property or setter ?
(using latest .NET Core 3 preview 8)
The default UI uses Razor Pages, and by convention, the URLs are based on the filesystem path, similar to how Web Forms used to work back in the day. In other words, that's the URL because the page is literally located at /Areas/Identity/Pages/Account/Login.cshtml (the Areas and Pages portions of the path are logical, and removed from the URL by convention, leaving just /Identity/Account/Login.
If you want to modify this, you'll need to specify custom routes, via something like:
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Identity/Account/Login", "Login");
});
You can also change the route on the actual page via the #page directive in the cshtml file:
`#page "Login"`
However, for the Identity UI, that approach would require you to scaffold the page into your project, obviously, in order to be able to change that.
For chaning Razor Page route, you could try Use a parameter transformer to customize page routes
Detail steps below:
IdentityParameterTransformer
public class IdentityParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null) { return null; }
// Slugify value
if (value.ToString().StartsWith("Identity"))
{
return $"/MyApp/{ value.ToString() }";
}
return value.ToString();
}
}
Register
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.Add(
new PageRouteTransformerConvention(
new IdentityParameterTransformer()));
});

Route to allow a parameter from both query string and default {id} template

I have an action in my ASP.Net Core WebAPI Controller which takes one parameter. I'm trying to configure it to be able to call it in following forms:
api/{controller}/{action}/{id}
api/{controller}/{action}?id={id}
I can't seem to get the routing right, as I can only make one form to be recognized. The (simplified) action signature looks like this: public ActionResult<string> Get(Guid id). These are the routes I've tried:
[HttpGet("Get")] -- mapped to api/MyController/Get?id=...
[HttpGet("Get/{id}")] -- mapped to api/MyController/Get/...
both of them -- mapped to api/MyController/Get/...
How can I configure my action to be called using both URL forms?
if you want to use route templates
you can provide one in Startup.cs Configure Method Like This:
app.UseMvc(o =>
{
o.MapRoute("main", "{controller}/{action}/{id?}");
});
now you can use both of request addresses.
If you want to use the attribute routing you can use the same way:
[HttpGet("Get/{id?}")]
public async ValueTask<IActionResult> Get(
Guid id)
{
return Ok(id);
}
Make the parameter optional
[Route("api/MyController")]
public class MyController: Controller {
//GET api/MyController/Get
//GET api/MyController/Get/{285A477F-22A7-4691-AA51-08247FB93F7E}
//GET api/MyController/Get?id={285A477F-22A7-4691-AA51-08247FB93F7E}
[HttpGet("Get/{id:guid?}"
public ActionResult<string> Get(Guid? id) {
if(id == null)
return BadRequest();
//...
}
}
This however means that you would need to do some validation of the parameter in the action to account for the fact that it can be passed in as null because of the action being able to accept api/MyController/Get on its own.
Reference Routing to controller actions in ASP.NET Core

How to retrieve RouteValues in ActionSelector in ASP.NET Core

I've created a GitHub repo to better understand the problem here. I have two actions on two different controllers bound to the same route.
http://localhost/sameControllerRoute/{identifier}/values
[Route("sameControllerRoute")]
public class FirstController : Controller
{
public FirstController()
{
// different EF Core DataContext than SecondController and possibly other dependencies than SecondController
}
[HttpGet("{identifier}/values")]
public IActionResult Values(string identifier, DateTime from, DateTime to) // other parameters than SecondController/Values
{
return this.Ok("Was in FirstController");
}
}
[Route("sameControllerRoute")]
public class SecondController : Controller
{
public SecondController()
{
// different EF Core DataContext than FirstController and possibly other dependencies than FirstController
}
[HttpGet("{identifier}/values")]
public IActionResult Values(string identifier, int number, string somethingElse) // other parameters than FirstController/Values
{
return this.Ok("Was in SecondController");
}
}
Since there are two matching routes, the default ActionSelector fails with:
'[...] AmbiguousActionException: Multiple actions matched. [...]'
which is comprehensible.
So I thought I can implement my own ActionSelector. In there I would implement the logic that resolves the issue of multiple routes via same logic depending on the 'identifier' route value (line 27 in code)
If 'identifier' value is a --> then FirstController
If 'identifier' value is b --> then SecondController
and so on...
protected override IReadOnlyList<ActionDescriptor> SelectBestActions(IReadOnlyList<ActionDescriptor> actions)
{
if (actions.HasLessThan(2)) return base.SelectBestActions(actions); // works like base implementation
foreach (var action in actions)
{
if (action.Parameters.Any(p => p.Name == "identifier"))
{
/*** get value of identifier from route (launchSettings this would result in 'someIdentifier') ***/
// call logic that decides whether value of identifier matches the controller
// if yes
return new List<ActionDescriptor>(new[] { action }).AsReadOnly();
// else
// keep going
}
}
return base.SelectBestActions(actions); // fail in all other cases with AmbiguousActionException
}
But I haven't found a good solution to get access to the route values in ActionSelector. Which is comprehensible as well because ModelBinding hasn't kicked in yet since MVC is still trying to figure out the Route.
A dirty solution could be to get hold of IHttpContextAccessor and regex somehow against the path.
But I'm still hoping you could provide a better idea to retrieve the route values even though ModelBinding hasn't happend yet in the request pipeline.
Not sure that you need to use ActionSelector at all for your scenario. Accordingly, to provided code, your controllers works with different types of resources (and so they expect different query parameters). As so, it is better to use different routing templates. Something like this for example:
FirstController: /sameControllerRoute/resourceA/{identifier}/values
SecondController: /sameControllerRoute/resourceB/{identifier}/values
In the scope of REST, when we are talking about /sameControllerRoute/{identifier}/values route template, we expect that different identifier means the same resource type, but different resource name. And so, as API consumers, we expect that all of the following requests are supported
/sameControllerRoute/a/values?from=20160101&to=20170202
/sameControllerRoute/b/values?from=20160101&to=20170202
/sameControllerRoute/a/values?number=1&somethingElse=someData
/sameControllerRoute/b/values?number=1&somethingElse=someData
That is not true in your case
I ended up implementing the proposed solution by the ASP.NET team. This was to implement an IActionConstrain as shown here:
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
namespace ActionConstraintSample.Web
{
public class CountrySpecificAttribute : Attribute, IActionConstraint
{
private readonly string _countryCode;
public CountrySpecificAttribute(string countryCode)
{
_countryCode = countryCode;
}
public int Order
{
get
{
return 0;
}
}
public bool Accept(ActionConstraintContext context)
{
return string.Equals(
context.RouteContext.RouteData.Values["country"].ToString(),
_countryCode,
StringComparison.OrdinalIgnoreCase);
}
}
}
https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.ActionConstraintSample.Web/CountrySpecificAttribute.cs