读取部分 URL 的 ASP.Net MVC 2 部分视图

发布于 2024-09-27 19:55:13 字数 290 浏览 1 评论 0原文

我正在使用控制器操作创建一个部分视图,例如:

public ActionResult GetPostsByUser(string userName) {

其中 userName 是 URL 的一部分:

www.example.com/User/toddM

toddM 是 userName

首先..我是否以正确的方式处理此问题?如果我将其设置为查询字符串 ?userName=toddM 它可以工作..但我需要它从 URL 读取。再说一遍,这是一个局部视图。谢谢!

i am creating a partialview with a controller action like:

public ActionResult GetPostsByUser(string userName)
{

where userName is part of the URL:

www.example.com/User/toddM

toddM being the userName

First off.. am i going about this the right way?? if i make it a querystring ?userName=toddM it works.. but i need it to read from the URL. Again, this is a partialview . thanks!

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

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

发布评论

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

评论(1

柠檬色的秋千 2024-10-04 19:55:13

您的 URL 示例不是“格式正确”,因为它缺少控制器/操作之一。

事实上,根据 MVC 项目模板创建的默认路由,您应该有

"{controller}/{action}/{id}", // URL with parameters

一个像 www.example.com/Post/User/toddM 这样的 URL 完全适合默认路由,所以我认为会工作没有任何问题。

这将是你在假设的 PostController 中的操作

public ActionResult User( string id )
{
    //id will contain toddM 
}

Your Url sample is not "well formed" as it miss one of the controller/action.

In fact, as per the default route created by the MVC project template you should have

"{controller}/{action}/{id}", // URL with parameters

An URL like www.example.com/Post/User/toddM would perfectly fits within the default route and so I think will work without any problem.

This one would be your action in an hypothetical PostController

public ActionResult User( string id )
{
    //id will contain toddM 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文