开发者

C# MVC 3 Root Route doesn't seem to work

开发者 https://www.devze.com 2023-02-10 19:56 出处:网络
EDIT: Sorry I explained it badly. Basically, in the below example, I want \"this-is-handled-by-content-controller\" to be the \"id\", so I can grab it in ContentController as an action parameter, but

EDIT: Sorry I explained it badly. Basically, in the below example, I want "this-is-handled-by-content-controller" to be the "id", so I can grab it in ContentController as an action parameter, but I want to access it via the root of the site, e.g mysite.com/this-is-not-passed-to-homecontroller.

I'm trying to create a root route that will go to a separate controller (instead of home).

I've followed the "RootController" example posted somewhere else that implements IRouteConstraint but it just doesn't seem to work and I've already wasted a couple of hours on this!

Basically, I have a LoginController, a HomeController, and a ContentController.

I want to be able to view HomeController/Index by going to http://mysite/. I want to be able to view LoginController/Index by going to http://mysite/Login. But.. I want the ContentController/Index to be called if any other result occurs, e.g: http:/mysite/this-is-handled-by-content-controller

Is there an elegant way to do this that works?

This was my last attempt.. I've cut/pasted/copied/scratched my head so many times its a bit messy:

routes.MapRoute(
            "ContentPages",
            "{action}",
            new { Area = "", controller = "ContentPages", action = "View", id = UrlParameter.Optional },
            new RootActionConstraint()
            );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { Area = "", controller = "Home", actio开发者_StackOverflow社区n = "Index", id = UrlParameter.Optional }, // Parameter defaults
            new string[] { "Website.Controllers" }
        );

Any help is appreciated greatly!

chem


I would do something similar to this, though that might not be the best solution if you keep adding more controller in the future.

routes.MapRoute(
    "HomePage",
    "",
    new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
    "Home",
    "home/{action}/{id}",
    new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
    "Login",
    "Login/{action}/{id}",
    new { controller = "Login", action = "Index", id="" }
);
//... if you have other controller, specify the name here

routes.MapRoute(
    "Content",
    "{*id}",
    new { controller = "Content", action = "Index", id="" }
);

The first route is for your youwebsite.com/ that call your Home->Index. The second route is for other actions on your Home Controller (yourwebsite.com/home/ACTION).

The 3rd is for your LoginController (yourwebsite.com/login/ACTION).

And the last one is for your Content->Index (yourwebsite.com/anything-that-goes-here).

public ActionResult Index(string id)
{
  // id is "anything-that-goes-here
}


Assuming you have ContentController.Index(string id) to handle routes matching the constraint, this should work:

routes.MapRoute(
        "ContentPages",
        "{id}",
        new { Area = "", controller = "Content", action = "Index" },
        new { id = new RootActionConstraint() }
        );

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}",
        new { Area = "", controller = "Home", action = "Index", id = UrlParameter.Optional },
        new string[] { "Website.Controllers" }
    );
0

精彩评论

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