.NET Core : Areas attribute route value not passed correctly - asp.net-core-2.2

Area route link with parameter returns
`/admin/home/edituser/b36c7c26`
instead of
`/admin/home/edituser?id=b36c7c26`
This in turns scatters my interface, meanwhile am using the standard ASP.NET tag helper link generator
asp-action="edituser" asp-controller="home" asp-area="admin" asp-route-id="#user.Id"
Am I doing something wrong?

The asp-route-{value} attribute enables a wildcard route prefix. Any value occupying the {value} placeholder is interpreted as a potential route parameter. If a default route isn't found, this route prefix is appended to the generated href attribute as a request parameter and value. Otherwise, it's substituted in the route template.
id is found in your route template so it is not appended as a request parameter and value. Your template is probably the typical default:
"{controller=Home}/{action=Index}/{id?}"
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-5.0

Related

System.Web.Http.Routing.UrlHelper creates a link with querystrings in Web API 2

When using WebAPI 2 and Attribute Routing, a call to UrlHelper.Link returns a route url with querystring attributes. The same code when using convention based routing returns the route wi the slashes as expected.
For example -
Attribute Routing: http://a.domain.com/api/foods?foodid=1
Convention Routing: http://a.domain.com/api/foods/1
Anyone know how to tell the UrlHelper to not use query strings when using attribute routing?
I am trying to convert some sample code from a pluralsight video into Web API 2.
You can get the code I am using here:
https://github.com/PriceIsByte/WebAPI/tree/issue/1/attribute_routing
Maybe you find the answer already. Here is mine though.
You must have an optional parameter in your route and also add a name to it. In your case it would be something like this
[Route("{foodid?}", Name="Foo")]
That will fix it.

URL parameters and backbone routing

Backbone.js maintains routing information in a URL after the hash mark, e.g.:
http://localhost:3000#page/hardware/table/?action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
Even though the routing information is in the format ?p1=v1&p2=v2&p3=v3, this portion is not technically part of the url query string since it comes after the hash mark.
My question is if I add an actual query string to our app's urls like this:
http://localhost:3000?newparam=newvalue#page/hardware/table/?action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
is there any possibility of the "newparam" url parameter interfering with the backbone portion?
the problem is your not actually creating a legit query string. your mixing your route with your parameters.
your example is formatted as:
domain ? param # route ? other params
as soon as a questionmark appears in a url everything after it is interpreted as a query string. (in this case) even your route.
personally i suggest using the html5 pushstate.
Backbone.history.start({pushState: true})
this will give you clean(er) urls
http://localhost:3000/page/hardware/table/?newparam=newvalue&action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
that will help your routes to not interfere with your parameters.

Twig - dynamically replace GET parameter's value

Is there a way to replace a GET parameter value from twig?
For example, I have a page at this address:
http://localhost/app_dev.php/test/?param1=40&sort=name
And in my twig I want to build 3 links like this:
http://localhost/app_dev.php/test/?param1=40&sort=name
http://localhost/app_dev.php/test/?param1=40&sort=address
http://localhost/app_dev.php/test/?param1=40&sort=code
For now I added the "&sort" parameter once again at the end on the URL, but this solution is actually a "patch" and it sucks!
address
In this example I only have 2 parameters, but in reality I have around 6 parameters, because the link that's generated it's obtained by submitting a .
This should solve your problem:
{{ path(app.request.attributes.get('_route'),
app.request.query.all|merge({'sort': 'address'})) }}
It gets the current route and all query parameters which are merged with the one you like to update before they are appended.
Symfony/Twig path function accept optional params. If these params are part of the route, they're handled by router but if they're not, they are passed as GET parameters.
So, if your corresponding route is, for example, my_route :
address

ASP.NET URL Routing with wildcard

I have a CMS system that I am using Routing to get the page name. I have the need to have unlimited values (sub directories, product names, different localizations) between the first item and the last item (page name).
For example:
/Products/Computers/ComputerType1/
And
/Productos/Ordenadores/ComputerType1/
Where ComputerType1 is the page name.
routes.Add(new Route("{*route}/{pageName}", routeHandler));
I cannot find a way to make the middle part ({*route}) of the route to be the wildcard so that unlimited number of sub directories can be put in front of the page name. Currently I have only been able to get around this with having a default wildcard route such as:
routes.Add(new Route("{*route}", routeHandler));
to catch everything. However, the wildcard seems to also be letting in gif urls even thou I have it specified as ignore above in the route code as:
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.Ignore("{resource}.asmx/{*pathInfo}");
routes.Ignore("{resource}.ashx/{*pathInfo}");
routes.Ignore("{resource}.gif/{*pathInfo}");
routes.Ignore("{resource}.png/{*pathInfo}");
routes.Ignore("{resource}.jpg/{*pathInfo}");
routes.Ignore("{resource}.ico/{*pathInfo}");
routes.Ignore("{resource}.pdf/{*pathInfo}");
routes.Ignore("{resource}.css/{*pathInfo}");
routes.Ignore("{resource}.js/{*pathInfo}");
Is there a better way of doing this? Should this be handled thru a custom route handler?
yes you should create a route handler for cases like these
simple create a class and derive it from RouteBase
override the GetRouteData method
in this method you can access the current httpcontext and thus you can access the requested URL
so u can route accordingly.
for more info on custom routes visit this link
http://www.codeproject.com/Articles/299531/Custom-routes-for-MVC-Application

URL routing documentation question

I'm reading about URL routing at How to: Define Routes for Web Forms Applications and there's something in the example I don't understand. If you look at the example provided below,
routes.MapPageRoute("", "SalesReport/{locale}/{year}/{*queryvalues}", "~/sales.aspx");
specifically at
"SalesReport/{locale}/{year}/{*queryvalues}"
Why does queryvalues have an asterisk in front of it and locale and year don't?
The * indicates a "catch all" parameter, which essentially matches everything else in the requested URL.
Everything after the "year" parameter in the URL will get dumped into the queryvalues parameter. So for example, the URL
http://whatever/SalesReport/canada/1999/x=1
will give you a queryvalues variable populated with "x=1". But it will also match the URL
http://whatever/SalesReport/canada/1999/x=1/y=2/z=3
and queryvalues will be populated with "x=1/y=2/z=3".
You can only have one catch-all parameter in your route, and it has to be the final parameter.

Resources