dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.49k stars 10.04k forks source link

[routing] 3.1 not working as expected. #19955

Closed Seabizkit closed 4 years ago

Seabizkit commented 4 years ago

here is a lot of info leading up to this point; https://github.com/dotnet/AspNetCore.Docs/issues/17320#issuecomment-600437381

considering this as the register for endpoints basically register areas, and overrides, then default

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers(); // Map attribute-routed API controllers

   //omitted for readability and breavity

    endpoints.MapAreaControllerRoute(
       name: "ImporterSourceTemplate_default",
       areaName: "Importer",
       pattern: "Org/{orgName}/Importer/Source/{controller=Template}/{action=Index}/{id?}", 
       constraints: new { area = "Importer", controller = "Template" }
       );

    endpoints.MapAreaControllerRoute(
       name: "Importer_default",
       areaName: "Importer",
       pattern: "Org/{orgName}/Importer/{controller=Dashboard}/{action=Index}/{id?}");

    endpoints.MapDefaultControllerRoute(); // Map conventional MVC controllers using the default route
    endpoints.MapRazorPages();
});

if we now focus on the overriding*..so finally i have something like

endpoints.MapAreaControllerRoute(
    name: "ImporterSource_default",
    areaName: "Importer",
    pattern: "Org/{orgName}/Importer/Source/{controller=Template}/{action=Index}/{id?}", 
    **  constraints: new { area = "Importer", controller = "Template" } **
);

but this doesn't let https://localhost:44356/Org/Paradox/Importer/Source where i have a Source Controller, and Index work and

endpoints.MapAreaControllerRoute(
    name: "ImporterSource_default",
    areaName: "Importer",
    pattern: "Org/{orgName}/Importer/Source/**Template**/{action=Index}/{id?}", 
    ** constraints: new { area = "Importer", controller = "Template" } **
);

still generates the wrong url 'https://localhost:44356/Org/Paradox/Importer/Template/Index/1' instead of 'https://localhost:44356/Org/Paradox/Importer/Source/Template/Index/1'

which for, on the Source/Index

<a asp-area="Importer" asp-controller="Template" asp-action="Index" asp-route-id="@item.Id">
            Templates <span class="badge badge-info">@item.TemplateCount</span>
</a>

generates url like:

https://localhost:44356/Org/sd/Importer/Source/Template/Index/9

if i have

endpoints.MapAreaControllerRoute(
    name: "ImporterSource_default",
    areaName: "Importer",
    pattern: "Org/{orgName}/Importer/Source/{controller=Template}/{action=Index}/{id?}", 
    **  constraints: new { area = "Importer", controller = "Template" } **
);

and then on the Template controller put

  [Area("Importer")]
    [Route("Org/{orgName}/[area]/Source/[controller]/[action]/{id?}")]
    //[Route("Org/{orgName}/Importer/Source/Tamplete/[controller]")]

    public class TemplateController : BaseController

then it will generate

https://localhost:44356/Org/Paradox/Importer/Source/Template/Index/1

but ideally its should pick up Index so should of been with Index omitted

https://localhost:44356/Org/Paradox/Importer/Source/Template/1

and cant not get that to work.

example url which I'm trying to make available with correct url generation

Area = "", controller=Home , action = Index / Area = "Importer", controller=Dashboard , action = Index /Org/Paradox/Importer

Area = "Importer", controller=Source, action = Index /Org/Paradox/Importer/Source Area = "Importer", controller=Source, action = Create /Org/Paradox/Importer/Source/Create Area = "Importer", controller=Source, action = Edit /Org/Paradox/Importer/Source/Edit/1 Area = "Importer", controller=Source, action = Delete /Org/Paradox/Importer/Source/Delete/2

Area = "Importer", controller=Template, action = Index /Org/Paradox/Importer/Source/Template/1 Area = "Importer", controller=Template, action = Create /Org/Paradox/Importer/Source/Template/Create/1 Area = "Importer", controller=Template, action = Delete /Org/Paradox/Importer/Source/Template/Delete/1

Area = "Importer", controller=FileSpecifcation, action = Index /Org/Paradox/Importer/Source/Template/FileSpecifcation/1

Area = "Importer", controller=TransformationSystemSpecs, action = Index /Org/Paradox/Importer/Source/Template/TransformationSystemSpecs/1

example of the url syntax in Source Index page

<a class="" href="@Url.Action("Index", "Template", new { Area = "Importer", id = item.Id })">
    Templates <span class="badge badge-info">@item.TemplateCount</span>
</a>

<a asp-area="Importer" asp-controller="Template" asp-action="Index" asp-route-id="@item.Id">
    Templates <span class="badge badge-info">@item.TemplateCount</span>
</a>

example of url syntax in Template Index

<td>@Html.ActionLink("File Specifcation", "Index", "FileSpecifcation", new { id = item.Id }, null)</td>
<td>@Html.ActionLink("System Specs", "Index", "TransformationSystemSpecs", new { id = item.Id }, null)</td>
<td>@Html.ActionLink("Outcome Specs", "Index", "TransformationOutcomeSpecs", new { id = item.Id }, null)</td>
</td>

I am having a really hard time, trying to get what I could get working in .net framework, in .net core.

Seabizkit commented 4 years ago

i understand it having a hard time trying to decide when the route is the route or the controller with url which have the word "Source" in it, but I'm trying to tell it how to work through this, and it seems like it just doesn't work.

Like i get that its finding this hard as to where its a route or must match on controller, but im registering it with more specific first and even telling it when it applies with the constraints

/Org/Paradox/Importer/Source <-- source is controller where /Org/Paradox/Importer/Source/Template/1 <-- source is just routing info here and template is the controlller

pranavkm commented 4 years ago

Attribute routes are preferred by the routing system over conventional routes (Map*). You could specify a route name to pick an exact template if you know the one you'd like to use.

ghost commented 4 years ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Seabizkit commented 4 years ago

so given the very detail explanation which i gave how would you make it work.

Seabizkit commented 4 years ago

@pranavkm please give working example i made the effort to give as much detail as possible and was simply met with "Attribute routes are preferred by the routing system over conventional routes" I spent some time trying to get this to work and couldn't I got Attribute routes working in dotnet framework 4.7.2 like your suggesting but could not in core.