Вопрос

I want to create a routing for this type of URL

http://myDomain.com/yr234ew

http://myDomain.com/234see

where the last part is some kind of ID that is passed to HomeController index

public class HomeController : Controller
    {
        public ActionResult Index(string id)
        {
            return View();
        }
    }

i tried something like

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

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

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

but its not working.. Can someone help me with this matter? thanks in advance

Это было полезно?

Решение

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

This won't make id required. Take a look at a similar questions here:

https://stackoverflow.com/a/7453891/546375

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top