ASP.NET MVC URL 路由

发布于 2024-12-06 05:45:57 字数 251 浏览 0 评论 0原文

我有一个问题,可以使用 ASP.NET MVC(Razor 和 MapRoute)来完成此操作吗?

示例

  • Category/ - 调用控制器类别和索引函数
  • Category/{State_name} - 调用控制器类别和 Cities(State_id) 函数,返回该州内的所有城市。

那么 URL 显示的是州名称,但城市功能接收州 id 吗?

I have a question, can this be done using ASP.NET MVC (Razor, and MapRoute)?

Example

  • Category/ - calls controller Category and Index function
  • Category/{State_name} - calls controller Category and Cities(State_id) function, returns all cities inside that state.

So URL is displaying state name , but Cities function receive state id?

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

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

发布评论

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

评论(2

南冥有猫 2024-12-13 05:45:57

是的,您可以,

public class CategoryController : Controller {

    // GET: /Category/
    // OR
    // GET: /Category/State_name
    public ActionResult Index(string State_name) {
        if (!string.IsNullOrWhiteSpace(State_name)) {
            int? State_id = FindStateIdFromStateName(State_name); // retrive state_id from datastore where state_name == State_name
            if (State_id.HasValue) { // means the currect state_name passed to action
                var model = FindStateById(State_id.Value);
                return View("Cities", model); // will renders Cities.cshtml with specified model
            } else {
                // means the specified state_name not found! u can do something else, or allow continue bellow lines
            }
        }
        return View(); // will render normal Index.cshtml
    }

}

如果您有任何问题或需要对任何部分进行澄清,请尝试告诉我。

更新

我的方式有一个问题!您可以通过 State_name 从 db 获取 ID,然后通过 State_name 从 db 获取模型!为什么不检索
第一次从 db by State_name 获取模型?
看:

public class CategoryController : Controller {

    // GET: /Category/
    // OR
    // GET: /Category/State_name
    public ActionResult Index(string State_name) {
        if (!string.IsNullOrWhiteSpace(State_name)) {
            var model = FindStateByName(State_name);
            if(model != null)
                return View("Cities", model);
            else
                // means the specified state_name not found! u can do something else, or allow continue bellow lines
        }
        return View(); // will render normal Index.cshtml
    }

}

如果你在 EF 上,那么你可以创建这个方法:

public State FindStateByName(state_name){
    using(var context = new YourEntityContext()){
         return context.States.SingleOrDefault(s => s.Name.ToLower() == state_name.ToLower());
    }
}

为什么不使用这种方式?

Yes u can, try

public class CategoryController : Controller {

    // GET: /Category/
    // OR
    // GET: /Category/State_name
    public ActionResult Index(string State_name) {
        if (!string.IsNullOrWhiteSpace(State_name)) {
            int? State_id = FindStateIdFromStateName(State_name); // retrive state_id from datastore where state_name == State_name
            if (State_id.HasValue) { // means the currect state_name passed to action
                var model = FindStateById(State_id.Value);
                return View("Cities", model); // will renders Cities.cshtml with specified model
            } else {
                // means the specified state_name not found! u can do something else, or allow continue bellow lines
            }
        }
        return View(); // will render normal Index.cshtml
    }

}

Let me know if you have any questions or need clarifications on any part.

UPDATE

I have one issue with the way! You get the ID from db with State_name, then get the model from db by State_name! Why not retrieve
model from db by State_name at the first time?
look:

public class CategoryController : Controller {

    // GET: /Category/
    // OR
    // GET: /Category/State_name
    public ActionResult Index(string State_name) {
        if (!string.IsNullOrWhiteSpace(State_name)) {
            var model = FindStateByName(State_name);
            if(model != null)
                return View("Cities", model);
            else
                // means the specified state_name not found! u can do something else, or allow continue bellow lines
        }
        return View(); // will render normal Index.cshtml
    }

}

and if you are on EF, then you can create this method:

public State FindStateByName(state_name){
    using(var context = new YourEntityContext()){
         return context.States.SingleOrDefault(s => s.Name.ToLower() == state_name.ToLower());
    }
}

Why not using this way?

柳絮泡泡 2024-12-13 05:45:57

这应该可以做到:

routes.MapRoute("Category_byState", "Category/{State_id}", new { controller = "Category", action = "Cities" });

This should do it:

routes.MapRoute("Category_byState", "Category/{State_id}", new { controller = "Category", action = "Cities" });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文