2

So I want to get my URLs to look something similar to this:

example.com, example.com/contact, example.com/sign-up, example.com/account

Instead of the default MVC way which would look like:

example.com, example.com/Home/Contact, example.com/Account/SignUp, example.com/Account/Account

All HomeController views work fine, but my AccountController doesn't.

When going to example.com/account I get an error saying:

The resource cannot be found.

This is my RouteConfig:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.LowercaseUrls = true;

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

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

And because I wanted to display the Account/SignUp view as /sign-up I added some custom code in the AccountController.

// GET: Account
public ActionResult Account()
{
    return View();
}

// GET: Sign Up
[ActionName("Sign-Up")]
public ActionResult SignUp()
{
    return View("SignUp");
}

Both formats /account or /account/account give me the same error from earlier. However /about, /contact, etc. does not.

Any help is appreciated, thanks!

P.S This is my folder structure:

> Views
-> Account
--> Account.cshtml
--> SignUp.cshtml
-> Home
--> About.cshtml
--> Contact.cshtml
--> Index.cshtml
2
  • 1
    Have you tried changing your route to something like this? url: "/Account/{action}/{id}", Commented Dec 24, 2019 at 11:07
  • 1
    @LukeAlderton that did the trick! Commented Dec 24, 2019 at 11:24

3 Answers 3

1

Route setup would need to be more explicit.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.LowercaseUrls = true;

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

    routes.MapRoute(
        name: "Default",
        url: "{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I realised I have to have it like account/sign-up rather than just sign-up. Which is a bit unfortunate but I could easily add it the HomeController if that's how I really wanted it.
1

Maybe custom route should be before default route

3 Comments

That sets the home page to be the Account page unfortunately.
Maybe you should look here for more informations: learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/…
This is from Microsoft documentation: The order of the routes that you add to the route table is important. Our new custom route is added before the existing Default route. If you reversed the order, then the Default route always will get called instead of the custom route.
0

Add RouteConfig.cs:

routes.MapMvcAttributeRoutes();

Add AccountController.cs as an attribute to the SignUp method

[Route("Sign-Up")]

Finally:

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

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

AccountController.cs

[Route("Sign-Up")]
public ActionResult SignUp()
{
  ViewBag.Message = "Your application description page.";

  return View();
}

Result

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.