0

I'm just starting out with .NET, and am building a test application. I currently have the homepage set using a DefaultController, and an Index() action method. This works as expected, and the homepage is simple www.domain.com.

I have created 2 new pages (Terms and Privacy) under the same DefaultController, using Terms() and Privacy() action methods.

I want to be able to browse to these with the URL as www.domain.com/terms and www.domain.com/privacy.

When i use a <li>@Html.ActionLink("Terms of Service", "Terms", "Default")</li> it works, but it takes me to the URL at www.domain.com/Default/privacy.

Should i be creating seperate controllers for each of these pages, or am I using the @html.ActionLink helper incorrectly? I have previously used <li><a href="~/privacy">Privacy Policy</a></li> but I understand this isn't best practice?

Also, is there a way to force links as lowercase?

My Controller Code:

public class DefaultController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Terms()
    {
        return View();
    }

    public ActionResult Privacy()
    {
        return View();
    }
}
Matt
  • 74,352
  • 26
  • 153
  • 180
Gavin5511
  • 791
  • 5
  • 31
  • 1
    you don't need another controller if you are putting those pages in the `DefaultController` and your helpers look correct for the actionlink – Grizzly Jul 21 '16 at 15:30
  • can you post your controller code? – Grizzly Jul 21 '16 at 15:31
  • Have added controller code above. It's currently all very basic – Gavin5511 Jul 21 '16 at 15:33
  • so when you hover over your `Terms` link, on the bottom left corner, you see `www.domain.com/Default/Privacy`? – Grizzly Jul 21 '16 at 15:34
  • Correct. And i'd expect to see www.domain.com/Privacy ideally. – Gavin5511 Jul 21 '16 at 15:35
  • If you want `.../Privacy` and `../Terms`, then you need to create specific routes for them (before the default route) - `routes.MapRoute( name: "Privacy", url: "Privacy", defaults: new {controller = "Default", action = "Privacy" })` –  Jul 25 '16 at 02:08

3 Answers3

3

If these were in the HomeController I don't believe you'd have the same problem. However, I think you can get around this by using the RouteConfig file:

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

routes.MapRoute(
    name: "Generic",
    url: "{controller}/{Action}/{Id}",
    defaults: new { Action = "Index", Id = UrlParameter.Optional }
);
ediblecode
  • 11,701
  • 19
  • 68
  • 116
  • I've tried this, and it does work for my example above, but then it changes the routing for all other controllers. Is there a way i can map a route JUST for the DefaultController and nothing else? – Gavin5511 Jul 21 '16 at 18:55
  • @Gavin5511 you can leave the default routing but define this first – ediblecode Jul 21 '16 at 19:44
  • Ok great. any chance you can update your answer to show how i can do this? – Gavin5511 Jul 21 '16 at 21:23
  • 1
    You cannot have `url: "{Action}/{Id}",` - thats just saying any url with 2 segments will go to the `Index()` method of `DefaultController` (the Default route will never be hit) –  Jul 25 '16 at 02:10
1

I believe that what you want to do is hide the controller name in the url. If that is the case, your question is answered here:

ASP.NET MVC - Removing controller name from URL

halfer
  • 19,824
  • 17
  • 99
  • 186
wayne.blackmon
  • 714
  • 13
  • 24
0

You can use Attribute routing to give specific routes to endpoints.

[Route("terms")]
public ActionResult Terms()
{
    return View();
}
[Route("privacy")]
public ActionResult Privacy()
{
    return View();
}

In your RouteConfig.cs you must enable attribute routing with the following line of code: routes.MapMvcAttributeRoutes();

Now any urls generated with @Url.Action() or @Html.ActionLink should generate URLS as domain.com/privacy and domain.com/terms

SlaterCodes
  • 1,139
  • 10
  • 21