Search code examples
.netasp.net-mvcrouteconfig

MVC Link inside a page


I am working in a .NET project with MVC and I need to make work a link that point to a section on the same page.

The following code works well without MVC:

<a href="#section1">Section 1</a>
<div id="section1">
  <h1>Section 1</h1>
</div>

Now this is my real URL: http://localhost:17338/MarketingCustomers/CleanData/1/1150119

And I need to be able to link with a div with id=customerErrorSection so the URL should looks like: http://localhost:17338/MarketingCustomers/CleanData/1/1150119#customerErrorSection

So I need to add "#customerErrorSection" at the end of the URL.

But the MVC routing changes the URL to http://localhost:17338/MarketingCustomers/CleanData/1/1150119#/customerErrorSection

I have been playing with the RouteConfig but I don't know how to create the URL I need, this is my code that isn't working:

routes.MapRoute(
                       name: "MarketingDataClean_CustomerErrorSection",
                       url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}/{#customerErrorSection}",
                       defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" }
                   );

Thanks for the help!


Solution

  • I don't really understand the problem here. Just leave the # part out.

    routes.MapRoute(name: "MarketingDataClean_CustomerErrorSection",
                    url: "MarketingCustomers/CleanData/{systemSourceId}/{systemSourceCustomerId}",
                    defaults: new { controller = "MarketingCustomers", action = "CleanData", systemSourceId = "", systemSourceCustomerId = "" });
    

    And then use the URL: http://localhost:17338/MarketingCustomers/CleanData/1/1150119#customerErrorSection

    If you want to use the Url.Action helper then use it like this:

    @Url.Action("CleanData", "MarketingCustomers", new { systemSourceId = 1, systemSourceCustomerId = 1150119})#customerErrorSection