Asp.net mvc 基于当前路由的操作

Asp.net mvc 基于当前路由的操作,asp.net-mvc,Asp.net Mvc,我想基于现有路由生成一个新的URL,但将添加一个新参数“page” 以下是一些示例: 旧的:~/localhost/something?what=2 新建:~/localhost/something?what=2&page=5 旧版:~/localhost/Shoes 新:~/localhost/Shoes/5 我不能仅仅将&page=5附加到现有url,因为路由可能不同 有些使用查询字符串,有些不使用。您可以通过RouteData对象拉出现有路由的部分来重建url。例如,以下内容将使用当前路由

我想基于现有路由生成一个新的URL,但将添加一个新参数“page”
以下是一些示例:

旧的:~/localhost/something?what=2
新建:~/localhost/something?what=2&page=5

旧版:~/localhost/Shoes
新:~/localhost/Shoes/5

我不能仅仅将&page=5附加到现有url,因为路由可能不同

有些使用查询字符串,有些不使用。

您可以通过
RouteData
对象拉出现有路由的部分来重建url。例如,以下内容将使用当前路由的控制器和操作呈现url:

<%=Url.RouteUrl(new { controller = ViewContext.RouteData.Values["controller"], 
                      action = ViewContext.RouteData.Values["action"] }) %>

为了让您开始,您可以使用类似于自定义扩展方法的方法来生成带有附加“page”参数的url。根据需要进行调整:

 public static string UrlWithPage(this UrlHelper urlHelper, string name, int page)
    {
        string url = urlHelper.RouteUrl(
            new { 
                    controller = urlHelper.RequestContext.RouteData.Values["controller"], 
                    action = urlHelper.RequestContext.RouteData.Values["action"], 
                    id = urlHelper.RequestContext.RouteData.Values["id"],
                    page = page 
                }
            );

        return "<a href=\"" + url + "\">" + name + "</a>";
    }
公共静态字符串UrlWithPage(此UrlHelper UrlHelper,字符串名称,int页)
{
字符串url=urlHelper.RouteUrl(
新{
controller=urlHelper.RequestContext.RouteData.Values[“controller”],
action=urlHelper.RequestContext.RouteData.Values[“action”],
id=urlHelper.RequestContext.RouteData.Values[“id”],
第页
}
);
返回“”;
}

这将基于路由配置构造一个格式正确的链接,无论页面是url中的实际段还是作为查询字符串追加。

我也遇到了类似的问题,并采取了扩展UrlHelper的方法。视图中的代码如下所示:

<a href="<%= Url.AddPage(2) %>">Page 2</a>
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Specialized;

public static class UrlHelperExtension
{
    public static string AddPage(this UrlHelper helper, int page)
    {

        var routeValueDict = new RouteValueDictionary
        {
            { "controller", helper.RequestContext.RouteData.Values["controller"] },
            { "action" , helper.RequestContext.RouteData.Values["action"]}
        };

        if (helper.RequestContext.RouteData.Values["id"] != null)
        {
            routeValueDict.Add("id", helper.RequestContext.RouteData.Values["id"]);
        }

        foreach (string name in helper.RequestContext.HttpContext.Request.QueryString)
        {
            routeValueDict.Add(name, helper.RequestContext.HttpContext.Request.QueryString[name]);
        }

        routeValueDict.Add("page", page);

        return helper.RouteUrl(routeValueDict);
    }
}

注意:我检查ID,因为我并不是在所有路线上都使用它。我在末尾添加了页面路由值,因此它是最后一个url参数(否则您可以在初始构造函数中添加它)。

这似乎是一个好方法:

// Clone Current RouteData
var rdata = new RouteValueDictionary(Url.RequestContext.RouteData.Values);

// Get QueryString NameValueCollection
var qstring = Url.RequestContext.HttpContext.Request.QueryString;

// Pull in QueryString Values
foreach (var key in qstring.AllKeys) {
    if (rdata.ContainsKey(key)) { continue; }
    rdata[key] = qstring[key];
}

// Update RouteData
rdata["pageNo"] = "10";

// Build Url
var url = Url.RouteUrl(rdata);

而且它避免了诸如?controller=example&action=problem等冲突。

路由中的参数数量不是恒定的。我可能有10个,也可能只有1个(在您的示例中是id)。