【问题标题】:MVC4 MapRoutingMVC4 地图路由
【发布时间】:2014-05-04 16:26:25
【问题描述】:

我想为这种类型的 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 }
            );
        }

但它不起作用..有人可以帮我解决这个问题吗?提前致谢

【问题讨论】:

    标签: c# asp.net-mvc-4 routes


    【解决方案1】:
     routes.MapRoute(
                name: "XX",
                url: "{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
            );
    

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

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

    【讨论】:

    • ozzzz .. 我之前尝试过,但我不知道为什么它不起作用,我用你的代码再次尝试它并且它有效.. 哈哈,谢谢 Alex..
    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多