ASP.NET MVC 5动态控制器路由 [英] ASP.NET MVC 5 dynamic controller routes

查看:95
本文介绍了ASP.NET MVC 5动态控制器路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找动态定义基于层次的路由的最佳方法方面,我遇到了挑战.

I'm having a challenge figuring out the best way to define my hierarchical based routes dynamically.

我正在寻求实现类似于以下内容的数据库驱动链接:

I'm looking to achieve database driven links similar to the below:

/Illinois/
/Illinois/Chicago/
/Illinois/Chicago/Id
/California/
/California/Los-Angeles/
/California/Los-Angeles/Id
/New-York/
/New-York/New-York-City/
/New-York/New-York-City/Id

以此类推,我不想为每个状态定义一个控制器,但是如果这是正确的方式",我并不是100%反对.

And so on, I don't want to have to define a controller for each state, but I'm not 100% against if if it's "the right way".

推荐答案

您可以创建类似HomeController的控制器,并在该控制器和相关操作的顶部使用路由属性来隐藏路由网址并调用您的位置和ID在这样的路线上:

You can create a controller like HomeController and use route attributes on the top of this controller and related action to hide the route url and call your locations and ids in route like this:

[RoutePrefix("")]
public class HomeController : Controller
{
    Route("{state?}/{city?}/{id?}")
    public ActionResult Index(string state, string city, int id)
    {
        //your codes
        return View();
    }
}

这篇关于ASP.NET MVC 5动态控制器路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆