MVC4 MapRouting

本文关键字:MapRouting MVC4 | 更新日期: 2023-09-27 18:01:31

我想为这种类型的URL创建一个路由

http://myDomain.com/yr234ew

http://myDomain.com/234see

最后一部分是传递给HomeController的某种ID索引

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

我试过像

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

但它不起作用…有人能帮我处理这件事吗?提前感谢

MVC4 MapRouting

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

这不会使id成为必需。看看这里的类似问题:

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