如何更改 ASP.net MVC URL
有没有一种简单的方法可以生成新的 Route-URL 而无需所有这些麻烦。
我目前正在处理通往同一视图的多条路线,所有路线均以 /{pagenumber}
我想做的就是删除所有内容回到最后一个反斜杠并替换数字。
我试图避免使用 URL Maker,因为它对于分页来说似乎有点矫枉过正,但欢迎所有建议…
这是我用于分页的方法,
#region Create View Pagination
/// <summary>
/// Create Pagination For Display
/// </summary>
/// <param name="TotalPgs"></param>
/// <param name="currentPg"></param>
/// <returns></returns>
private string pagnationStr(int TotalPgs, int currentPg)
{
string tmp = "";
string URLX = "";
URLX = string.Format("{0}", base.Request.Url);
int pageSet = 10;
//Previous Link
if (currentPg > 1) tmp += "<li><a href='" + URLX + (currentPg - 1) + "'>Previous</a></li>";
//Standard Pagination
tmp += "<ul class=\"pagination\">";
for (int i = currentPg; i < TotalPgs; i++)
{
tmp += "<li><a href='" + URLX + i + "'>" + i.ToString() + "</a></li>";
if (pageSet < 1) break;
pageSet--;
}
tmp += "</ul>";
return tmp;
}
#endregion
- 其中的各个部分通常来自应用程序设置,我已将它们替换为数字以完成操作。
Is there a simple way of generating a new Route-URL without all this faff.
I'm currently Handling several routes to the same view, all of which end with
/{pagenumber}
All I'm trying to do is delete everything back to the last backslash and replace the number.
I was trying to avoid using the URL Maker as it seems a tad overkill for pagination but all suggestions welcome…
Heres the method im using for pagination
#region Create View Pagination
/// <summary>
/// Create Pagination For Display
/// </summary>
/// <param name="TotalPgs"></param>
/// <param name="currentPg"></param>
/// <returns></returns>
private string pagnationStr(int TotalPgs, int currentPg)
{
string tmp = "";
string URLX = "";
URLX = string.Format("{0}", base.Request.Url);
int pageSet = 10;
//Previous Link
if (currentPg > 1) tmp += "<li><a href='" + URLX + (currentPg - 1) + "'>Previous</a></li>";
//Standard Pagination
tmp += "<ul class=\"pagination\">";
for (int i = currentPg; i < TotalPgs; i++)
{
tmp += "<li><a href='" + URLX + i + "'>" + i.ToString() + "</a></li>";
if (pageSet < 1) break;
pageSet--;
}
tmp += "</ul>";
return tmp;
}
#endregion
- various bits of this normally come from the app-settings I've replaced them with numbers to be complete.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
路由有两种工作方式 - 它将接收请求 URL 并匹配正确的资源,或者您可以要求路由根据您拥有的参数生成 URL。
这将根据您的参数值生成您想要的资源的 URL。
如果您传入“controller”=“whatever”、“action”=“whatever”和“pagenumber”= currentpg,则应该获得所需的 URL,而无需手动解析 URL。 如果您的路线将来发生变化,这也更具可扩展性。
传递给 GetVirtualPath 且路由匹配未使用的任何额外参数都将作为查询字符串参数附加。
Routing works two ways - it will take in a request url and match the correct resource, or you can ask routing to generate you an URL based on the parameters you have.
This will generate you a URL to the resource you'd like, based on your parameter values.
If you pass in "controller" = "whatever", "action" = "whatever", and "pagenumber" = currentpg, you should get the URL that you want, without having to manually parse the URLs. This is also more scalable should your routes change in the future.
Any extra parameters you pass into GetVirtualPath that are not consumed by the route matching will be appended as querystring parameters.