Url.Action基于当前路线

时间:2022-11-30 21:22:37

I'd like to generate a new URL based on the existing route, but will add a new parameter 'page'
Here are a few examples:

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

old: ~/localhost/something?what=2
new: ~/localhost/something?what=2&page=5

old:〜/ localhost / something?what = 2 new:〜/ localhost / something?what = 2&page = 5

old: ~/localhost/Shoes
new: ~/localhost/Shoes/5

老:〜/ localhost /鞋子新:〜/ localhost / Shoes / 5

I can not just append &page=5 to existing url because routes may be different.
Some use the query string and some do not.

我不能只将&page = 5附加到现有网址,因为路由可能不同。有些使用查询字符串,有些则不使用。

3 个解决方案

#1


6  

I had a similar issue, and took the approach of extending the UrlHelper. The code in the View looks like:

我有一个类似的问题,并采取扩展UrlHelper的方法。视图中的代码如下所示:

<a href="<%= Url.AddPage(2) %>">Page 2</a>

The UrlHelper extension looks like:

UrlHelper扩展名如下:

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);
    }
}

A couple of notes: I check for the ID, since I don't use it in all my routes. I add the Page route value at the end, so it is the last url parameter (otherwise you could add it in the initial constructor).

几个注释:我检查ID,因为我没有在我的所有路线中使用它。我在最后添加了Page route值,因此它是最后一个url参数(否则你可以在初始构造函数中添加它)。

#2


1  

This seems like a good approach:

这似乎是一个很好的方法:

// 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);

and it avoids collisions such as ?controller=example&action=problem etc.

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

#3


0  

You could reconstruct a url by pulling out the parts of the existing route by way of the RouteData object. For instance, the following would render a url with the controller and action of the current route:

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

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

To get you started, you could go with something like a custom extension method that generates the url with an additional "page" parameter. Adjust as necessary:

为了帮助您入门,您可以使用类似自定义扩展方法的方法来生成带有额外“页面”参数的网址。根据需要调整:

 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>";
    }

This will construct a properly formatted link based on the routing configuration, whether page is real segment in the url or just appended as a querystring.

这将构建基于路由配置的格式正确的链接,无论页面是URL中的真实段还是仅作为查询字符串附加。

#1


6  

I had a similar issue, and took the approach of extending the UrlHelper. The code in the View looks like:

我有一个类似的问题,并采取扩展UrlHelper的方法。视图中的代码如下所示:

<a href="<%= Url.AddPage(2) %>">Page 2</a>

The UrlHelper extension looks like:

UrlHelper扩展名如下:

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);
    }
}

A couple of notes: I check for the ID, since I don't use it in all my routes. I add the Page route value at the end, so it is the last url parameter (otherwise you could add it in the initial constructor).

几个注释:我检查ID,因为我没有在我的所有路线中使用它。我在最后添加了Page route值,因此它是最后一个url参数(否则你可以在初始构造函数中添加它)。

#2


1  

This seems like a good approach:

这似乎是一个很好的方法:

// 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);

and it avoids collisions such as ?controller=example&action=problem etc.

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

#3


0  

You could reconstruct a url by pulling out the parts of the existing route by way of the RouteData object. For instance, the following would render a url with the controller and action of the current route:

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

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

To get you started, you could go with something like a custom extension method that generates the url with an additional "page" parameter. Adjust as necessary:

为了帮助您入门,您可以使用类似自定义扩展方法的方法来生成带有额外“页面”参数的网址。根据需要调整:

 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>";
    }

This will construct a properly formatted link based on the routing configuration, whether page is real segment in the url or just appended as a querystring.

这将构建基于路由配置的格式正确的链接,无论页面是URL中的真实段还是仅作为查询字符串附加。