DmitryEfimenko / TwitterBootstrapMvc

Fluent implementation of ASP.NET-MVC HTML helpers for Twitter Bootstrap.
Apache License 2.0
222 stars 79 forks source link

Incorrect url in ActionLinkButton with RouteName #414

Closed PaulMartinsen closed 8 years ago

PaulMartinsen commented 8 years ago

I don't seem to be getting the correct url if I try to reference a named route. So when I use

@Html.Bootstrap().ActionLinkButton("Edit", "").RouteName(ManageSitesControllerRoute.GetEditSite).RouteValues(new { id = "test" })

I get a button with a destination address of: http://localhost:49518/home/manage-sites/edit-site/test/test/

When I use: @Html.RouteLink("Edit", ManageSitesControllerRoute.GetEditSite, new { id = "test" }) I get an ordinary link with the (expected) destination address of: http://localhost:49518/manage-sites/edit-site/test/

It appears to have added the "home" part to the path and an extra copy of the route variable. Do I need to do something else to use named routes?

DmitryEfimenko commented 8 years ago

Thanks for submitting this bug. Was kind of surprised nobody stumbled across this before. In any case, this is fixed. Please get latest.

PaulMartinsen commented 8 years ago

Thanks for tackling this so quickly; it is much appreciated!

I pulled the latest. Now when I have: @Html.RouteLink("Add User", AccountManagerControllerRoutes.GetNewUser)

@Html.Bootstrap().ActionLinkButton("Add User", "").RouteName(AccountManagerControllerRoutes.GetNewUser)

The first generates the expected link, but the second generates an empty href.

If I use @{ var b = Html.Bootstrap().ActionLinkButton("Add User", "").RouteName(AccountManagerControllerRoutes.GetNewUser); }

I can inspect b and see: image

So the ActionName property has the correct link url. But unfortunately the rendered anchor tag doesn't appear to get it.

DmitryEfimenko commented 8 years ago

Could you please show me what your route AccountManagerControllerRoutes.GetNewUser looks like? I haven't done much RouteLinks in MVC development, so it's a bit of an uncharted area for me. But we'll get it fixed!

PaulMartinsen commented 8 years ago

New for me too. I haven't done much in ASP.Net and found this template that seems to have a lot of useful things built-in: https://visualstudiogallery.msdn.microsoft.com/6cf50a48-fc1e-4eaf-9e82-0b2a6705ca7d Has worked straight out of the box.

Then AccountManagerControllerRoutes.GetNewUser is a string constant equal to "AccountManagerGetNewUser" that names the route in some lookup table (they have to be unique).

The controller and methods get attributes: [Authorize(Roles = StandardRoleNames.Administrator)] [RoutePrefix("AccountManager")] public class AccountManagerController : Controller { : : [HttpGet] [Route("new-user", Name = AccountManagerControllerRoutes.GetNewUser)] public ActionResult NewUser() { var Model = new EditAccountModel { AccountLocked = false, DailyReport = true }; return View(Model); } :

And the routes are setup like this: public static void RegisterRoutes(RouteCollection routes) { : : // Enable attribute routing. routes.MapMvcAttributeRoutes();

  // Normal routes are not required because we are using attribute routing. So we don't need this MapRoute 
  // statement. Unfortunately, Elmah.MVC has a bug in which some 404 and 500 errors are not logged without 
  // this route in place. So we include this but look out on these pages for a fix:
  // https://github.com/alexbeletsky/elmah-mvc/issues/60
  // https://github.com/RehanSaeed/ASP.NET-MVC-Boilerplate/issues/8

if false

  routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

endif

I disabled the MapRoute as that seemed to be making the problem worse & I could live without some of that error logging to use your add-in.

Is it possible that after resolving the route and placing it in ActionName that it tries to look up a route for that action name? It would come up empty.

PaulMartinsen commented 8 years ago

Hello again, I put a simple project that illustrates the problem here: https://www.dropbox.com/s/68tk3p88x3ey9wg/WebApplication1.zip?dl=0

DmitryEfimenko commented 8 years ago

The demo helped a lot, thanks. This should be fixed. Please get latest.

PaulMartinsen commented 8 years ago

Thanks Dmitry. That seems to have solved it. I appreciate all your work on this project. It makes building sites much faster!