2

I've question about routing in MVC i've created a simple website i defined a controller called HomeController.cs

defined my public ActionResult WebPage(int id) the id is the Id of the page i'm displaying my route will look something like:

Current:

http://localhost:5000/Home/WebPage/1

What i want is:

http://localhost:5000/1

My current RouteConfig.cs

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

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

I've tried allot with different routes in RouteConfig.cs but im stuck can someone point me into the right direction?

I've searched on StackOverflow something similer to this but didn't find.

Stefan van de Laarschot
  • 2,154
  • 6
  • 30
  • 50
  • Maybe, define your action as `public ActionResult WebPage(int id)` and add a root route: `routes.MapRoute(name: "Root", url: "{id}", defaults: new { controller = "Home", action = "WebPage" });` – Felipe Oriani Jun 28 '17 at 12:55
  • You cant (unless you were to create a route constraint so if the route value is an `int` it will use that method) - you need something to uniquely identify your routes. –  Jun 28 '17 at 12:57
  • Found my answhere here ! https://stackoverflow.com/questions/35860959/asp-net-mvc-5-dynamic-controller-routes – Stefan van de Laarschot Jun 29 '17 at 13:46

3 Answers3

2

You should try route attribute, it may help you

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 });
}

Use attribute like this

  [Route(“{id:int}”)]
  public ActionResult WebPage(int id) { … }
Sarbanjeet
  • 253
  • 1
  • 15
1

What Felipe proposes will work, if you apply a constraint on the id value.

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "WebPage",
            url: "{id}",
            defaults: new { controller = "Home", action = "WebPage" },
            constraints: new { id = @"\d+" });

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Rafał Rutkowski
  • 1,419
  • 10
  • 11
  • what if the id is a string value? – Stefan van de Laarschot Jun 29 '17 at 12:22
  • The constraint is not satisfied, so the application will try to match the next route map, in this case the `Default`. For example, given the URL `http://hostname/x`, the `Default` map actually matches, so the application will try to execute the `Index` action of the `XController`. – Rafał Rutkowski Jun 29 '17 at 15:56
1

The answer

in my controller: [RoutePrefix("")] and my Method WebPage [Route("{id?}")]

[RoutePrefix("")]
public class HomeController : Controller
{
    [Route("{id?}")]
    public ActionResult WebPage(string id)
    {
       return View();
    }
}

RouteConfig.cs

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

          routes.MapRoute(
               name: "WebPage",
               url: "{id}",
               defaults: new { controller = "Home", action = "WebPage" }
            );

            routes.MapRoute(
                    name: "Default",
                    url: "{action}/{id}",
                    defaults: new { controller = "Home", action = "Index",id = UrlParameter.Optional }
            );
        }
}
Stefan van de Laarschot
  • 2,154
  • 6
  • 30
  • 50