0

I have an MVC 6 project that has routing set up as:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}/{closeOnSubmit?}");
            });

I'm attempting to build a hyperlink using tag helpers like:

<a asp-controller="MyController" asp-action="Create" asp-route-closeOnSubmit="true" class="fa fa-plus indent-15">+</a>

When the page is created and I mouse over the link, it's reporting as http://localhost/MyController/Create without the closeOnSubmit parameter. When I examine the HTML in Chrome's developer tools, it looks like:

<a href class="fa fa-plus indent-15">+</a>

What do I need to do to get the closeOnSubmit parameter to be properly appended to the route?

Edit:
I don't fully understand MVC routing, so unfortunately gave some erroneous information. When I first created my app, it had the default route of:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

When trying to get this working, I thought that maybe it wasn't routing correctly because I didn't have closeOnSubmit added into the route. After reading Darin's answer, I understand it and definitely had it wrong after adding it to the route. However, for this particular route, I don't need an id passed. I tried changing the default route so that it used closeOnSubmit rather than id and it still didn't fix the issue of links not being created correctly.

While troubleshooting, I also found that the routes are even more wrong than I thought. The page I am working on is generated by going to http://localhost/ControllerOne/Create. The page that the hyperlink should point to is http://localhost/ControllerTwo/Create'. Even though my asp-controller tag is set to ControllerTwo, all of my hyperlinks are showing ashttp://ControllerOne/Create`. I have a feeling that that's just because of the empty href attribute, but I'd like to know what's causing the href attribute to be empty.

2 Answers 2

0

Only the last parameter of a route can be optional. Otherwise the routing engine simply cannot disambiguate your routes.

Think about it, when the routing engine encounters this url:

http://localhost/MyController/Create/true

does the true token should be bound to the id parameter or the closeOnSubmit parameter?

So make sure that your id parameter is mandatory:

template: "{controller=Home}/{action=Index}/{id}/{closeOnSubmit?}"

and that you have specified a value for it when building your url:

<a asp-controller="MyController" asp-action="Create" asp-route-id="1234" asp-route-closeOnSubmit="true" class="fa fa-plus indent-15">+</a>
Sign up to request clarification or add additional context in comments.

Comments

0

Wow, PEBKAC.

The problem came in because the controller I was referencing didn't exist yet. Which, IIRC, would cause an error in MVC5 rather than creating an empty HREF tag. Either way I should have been smart enough to see that one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.