开发者

How do link the first level like localhost/About and localhost/Faq

I make Views/About/Index.aspx and Views/Faq/Index.aspx + Controllers/AboutController.cs and Controllers/FaqController.cs controllers for these purposes.

I want to have one controller Controllers/DefaultController.cs + Views/About.aspx and Views/Faq.aspx in the 开发者_如何学JAVAroot, for example.

How to set it up?


You can do something like this:

 private static readonly string[] StaticPages = 
                                   {
                                       "FaQ", "Help"
                                   };

    /// <summary>
    /// Registers the routes.
    /// </summary>
    /// <param name="routes">The routes.</param>
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("StaticPages",
            "{id}", // url
            new { controller = "StaticPages", action = "Show" }, // defaults
            new { id = String.Join("|", StaticPages) }); // constraints


        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // Parameter defaults
    }


No matter what, your *.aspx files for the Views should stay in the Views/{Name}/{PageName}.aspx style.

If you want a URL like http://localhost/About, you set up a default route in Global.asax.cs to point to the proper Action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜