C# 用户输入正斜杠“/&引用;在asp.NETMVC3URL中
使用.Net 4,难道不能在路由参数中包含正斜杠吗?或者应该强制用户使用连字符 例如:C# 用户输入正斜杠“/&引用;在asp.NETMVC3URL中,c#,asp.net,asp.net-mvc,asp.net-routing,C#,Asp.net,Asp.net Mvc,Asp.net Routing,使用.Net 4,难道不能在路由参数中包含正斜杠吗?或者应该强制用户使用连字符 例如: mysite.com/user/{id}/{year}映射到 mysite.com/user/01/2012 其中{id}={user/01}和{year}=2012在上述示例中 如果这是最后一个路线参数,则会更容易,如下面的答案所示。但事实并非如此 我读过这些,和我的问题一样 但我并没有真正得到我想要的。如果我想生成URL,我可以使用这个解决方案 但是如果用户在浏览器中键入url会发生什么情况?
mysite.com/user/{id}/{year}
映射到
mysite.com/user/01/2012
其中{id}={user/01}
和{year}=2012
在上述示例中
如果这是最后一个路线参数,则会更容易,如下面的答案所示。但事实并非如此
我读过这些,和我的问题一样
http://localhost:59025/User/mhis/001/2012
Global.asax-定义路线:
“年”参数设置为可选,因此如果用户类型http://localhost:59025/User/mhis/001/2012
,年份为2012年,如果不键入任何内容,年份将为空
虚拟用户类:
如果我没弄错的话。
用户ID为“mhis/001”,年份为2012年,那么您希望使用如下URL:
http://localhost:59025/User/mhis/001/2012
Global.asax-定义路线:
“年”参数设置为可选,因此如果用户类型http://localhost:59025/User/mhis/001/2012
,年份为2012年,如果不键入任何内容,年份将为空
虚拟用户类:
那么,这里的实际问题是什么?试着列出你想要的和实际发生的事情。我已经用粗体表示了疑问。应该强制用户使用连字符还是允许用户使用“/”。或者可以在请求解析为路由参数之前拦截请求吗?谢谢,我想你还不太清楚你想要实现什么?如果用户键入无效的URL,您可以使用友好的404响应,该404解释这不是有效的URL,并包含指向用户搜索表单的链接。或者给我们一个你想使用的url的例子。这个问题很清楚,其中包括了一些例子-我更新了我的问题,那么这里的实际问题是什么?试着列出你想要的和实际发生的事情。我已经用粗体表示了疑问。应该强制用户使用连字符还是允许用户使用“/”。或者可以在请求解析为路由参数之前拦截请求吗?谢谢,我想你还不太清楚你想要实现什么?如果用户键入无效的URL,您可以使用友好的404响应,该404解释这不是有效的URL,并包含指向用户搜索表单的链接。或者给我们一个你想使用的url的例子。这个问题很清楚,其中包括了一些例子-我更新了我的问题你的意思是我应该告诉我的用户用“-”替换“/”吗?如果这是最后一个解决方案,它可能是我文档的一部分。为什么/在哪里您的用户首先会键入“/”?他们的用户id类似于
mhis/2012/001
我无法控制这一点,除非我告诉他们每次用户不应该键入(复杂的)URL时交换“/”for“-”。工具应该创建这些URL并使用正确的编码(-
就可以了)。你的意思是我应该告诉我的用户用“-”替换“/”吗?如果这是最后一个解决方案,它可能是我文档的一部分。为什么/在哪里您的用户首先会键入“/”?他们的用户id类似于mhis/2012/001
我无法控制这一点,除非我告诉他们每次用户不应该键入(复杂的)URL时交换“/”for“-”。工具应该创建这些URL并使用适当的编码(-
就可以了)。
routes.MapRoute(
"UserDetails", // Route Name
"User/{idpart1}/{idpart2}/{year}", // URL with parameters
new { controller = "User", action = "Details", year= UrlParameter.Optional} // Parameter defaults
);
public class User
{
public String Id { get; set; }
public String Name { get; set; }
/// <summary>
/// just a helper to get the first part of the userID
/// </summary>
public string IDPart1 { get { return Id.Split('/')[0]; } }
/// <summary>
/// just a helper to get the second part of the userID
/// </summary>
public string IDPart2 { get { return Id.Split('/')[1]; } }
}
public class UserController : Controller
{
/// <summary>
/// user list...
/// </summary>
List<User> _users = new List<User> {
new User{ Id="mhis/001", Name="John Smith"},
new User{ Id="mhis/002", Name="Some Body Else"}
};
/// <summary>
/// This is actually a user list, but...
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
return View(_users);
}
public ActionResult Details(string idpart1, string idpart2, int? year)
{
//do what you have to do with the year...
ViewBag.year = year;
string realUserID = String.Format(@"{0}/{1}", idpart1, idpart2);
var user = _users.FirstOrDefault(u => u.Id == realUserID);
return View(user);
}
}
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Details", "Details", new { idpart1 = item.IDPart1, idpart2 = item.IDPart2 })
</td>
</tr>
}
@model MvcApplication1.Controllers.User
@{
ViewBag.Title = "Details";
}
@if(Model != null) {
<h2>Details</h2>
<fieldset>
<legend>User</legend>
The year is: "@ViewBag.year"
<div class="display-label">Name</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
</fieldset>
<p>@Html.ActionLink("Back to List", "Index")</p>
} else {//this should be handled in the controller...
<h2>User not found!</h2>
}