C# ASP.net MVC路由参数
我有这样的路线:C# ASP.net MVC路由参数,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我有这样的路线: http://localhost:1936/user/someOne/##feed%233dbfc1b3-44b4-42b4-9305-4822ac151527 routes.MapRoute("Profile", "user/{userName}/{treningId}", new { controller = "Profile", action = "Index", userName = UrlParameter.Optional, tren
http://localhost:1936/user/someOne/##feed%233dbfc1b3-44b4-42b4-9305-4822ac151527
routes.MapRoute("Profile", "user/{userName}/{treningId}",
new { controller = "Profile", action = "Index", userName = UrlParameter.Optional, treningId = UrlParameter.Optional }
);
public ActionResult Index(string username, string treningId)
{
//more code here
}
我的路由配置如下:
http://localhost:1936/user/someOne/##feed%233dbfc1b3-44b4-42b4-9305-4822ac151527
routes.MapRoute("Profile", "user/{userName}/{treningId}",
new { controller = "Profile", action = "Index", userName = UrlParameter.Optional, treningId = UrlParameter.Optional }
);
public ActionResult Index(string username, string treningId)
{
//more code here
}
我的控制器中的操作如下:
http://localhost:1936/user/someOne/##feed%233dbfc1b3-44b4-42b4-9305-4822ac151527
routes.MapRoute("Profile", "user/{userName}/{treningId}",
new { controller = "Profile", action = "Index", userName = UrlParameter.Optional, treningId = UrlParameter.Optional }
);
public ActionResult Index(string username, string treningId)
{
//more code here
}
但我无法获取此部分:##提要%233dbfc1b3-44b4-42b4-9305-4822ac151527
,当我调试代码时,我看到treningId为空
我的问题是如何在##之后获得URL的一部分 你不能
浏览器在第一次之后根本不会将内容发送到服务器(您可以使用浏览器中的调试器检查浏览器和服务器之间的通信量来验证这一点)。那是因为
你为什么不干脆
- 使用常规查询参数(例如
http://localhost:1936/user/someOne?myparameter=feed%233dbfc1b3-44b4-42b4-9305-4822ac151527),或者
- 选择除“##”之外的另一个分隔符
- 使用常规查询参数(例如
http://localhost:1936/user/someOne?myparameter=feed%233dbfc1b3-44b4-42b4-9305-4822ac151527),或者
- 选择除“##”之外的另一个分隔符
- 你不能
浏览器在第一次之后根本不会将内容发送到服务器(您可以使用浏览器中的调试器检查浏览器和服务器之间的通信量来验证这一点)。那是因为
你为什么不干脆
- 这是不可能的,因为请求中不包括哈希;它们保留在URL中供客户端使用(通常用于标记文档/部分中的特定位置)
你有两个选择。将哈希字符编码为
%23
:
%23%23feed%233dbfc1b3-44b4-42b4-9305-4822ac151527
或者使用不同的字符/路由。这是不可能的,因为请求中不包含哈希;它们保留在URL中供客户端使用(通常用于标记文档/部分中的特定位置) 你有两个选择。将哈希字符编码为
%23
:
%23%23feed%233dbfc1b3-44b4-42b4-9305-4822ac151527
或者使用不同的字符/路径。按照编写URL的方式,浏览器不会提交URL的任何部分-哈希部分。因此,您的MVC应用程序将永远不会收到该值,因此将始终将其设置为
null
要使该值对MVC可用,请转义
#
,以避免它们标记URL片段部分的开头。按照编写URL的方式,浏览器不会提交URL的任何部分-哈希部分。因此,您的MVC应用程序将永远不会收到该值,因此将始终将其设置为null
要使该值对MVC可用,请转义#
,以避免它们标记URL片段部分的开头