如何更改 ASP.net MVC URL

发布于 2024-07-27 15:47:31 字数 1199 浏览 3 评论 0原文

有没有一种简单的方法可以生成新的 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 技术交流群。

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

发布评论

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

评论(1

ㄖ落Θ余辉 2024-08-03 15:47:31

路由有两种工作方式 - 它将接收请求 URL 并匹配正确的资源,或者您可以要求路由根据您拥有的参数生成 URL。

RouteCollection.GetVirtualPath(
                                context, 
                                new RouteValueDictionary(
                                       {param1name, param1value},
                                       {param2name, param2value}...
                                 )
                               );

这将根据您的参数值生成您想要的资源的 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.

RouteCollection.GetVirtualPath(
                                context, 
                                new RouteValueDictionary(
                                       {param1name, param1value},
                                       {param2name, param2value}...
                                 )
                               );

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.

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