MVC3 使用参数路由到特定控制器

发布于 2024-12-20 21:47:07 字数 570 浏览 2 评论 0原文

我正在尝试使其成为可能,因此如果有人尝试访问 /content,他们将被发送到特定的控制器(在本例中为内容,然后 url 中的以下详细信息作为参数传递。url

http: //localhost:51118/content/1/7/test.html

我已经尝试过:

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

我试图集成一个cms,这就是为什么url是这样的

编辑:

public class ContentController : Controller
{    
        public ActionResult Index()
        {
            var model = new Content();     
             return View(model);
        }
}

I am trying to make it possible so if someone tryes to go to /content they are sent to a specfic controller (in this case content and then the following details after in the url are passed as parameters.

the url is http://localhost:51118/content/1/7/test.html

i have tried:

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

im trying to integrate a cms thats why the url is what it is

EDIT:

public class ContentController : Controller
{    
        public ActionResult Index()
        {
            var model = new Content();     
             return View(model);
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2024-12-27 21:47:07

你的路线不正确。仅当您访问 http://localhost:51118/content url 时,您才会被路由到内容控制器。如果您想指定更多参数,您应该在路线中添加更多部分。
routes.MapRoute("content", "content/{id1}/{id2}/{content}", new {controller = "Content", action = "Index"});


public class ContentController : Controller
{    
        public ActionResult Index(int id1, int id2, string content)
        {
            var model = new Content();     
             return View(model);
        }
}

Your route is not correct. You will get routed to the Content controller only when you will access the http://localhost:51118/content url. If you want to specify more paramateres you should add more sections to your route.
routes.MapRoute("content", "content/{id1}/{id2}/{content}", new { controller = "Content", action = "Index"});


public class ContentController : Controller
{    
        public ActionResult Index(int id1, int id2, string content)
        {
            var model = new Content();     
             return View(model);
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文