ASP.NET MVC、查询字符串、路由和默认绑定器,如何组合它们?

发布于 2024-10-21 15:09:18 字数 1744 浏览 8 评论 0原文

我有一个使用强类型视图的 ASP.NET MVC 站点。就我而言,控制器操作可能如下所示:

public ActionResult List(MyStrongType data)

提交页面(视图)时,响应将生成一个如下所示的 URL(是的,我知道路由可以生成更好的 URL):

http://localhost/Ad/List?F.ShowF=0&ALS.CP=30&ALS.L=0&ALS.OB=0&ALS.ST=0&S=&LS.L1=&LS.L2=&CS.C1=32&CS.C2=34&CS.C3=&ALS.ST=0

如果我再次提交页面,我可以看到操作中的数据对象已正确设置(根据 URL)(默认活页夹)。

问题是:假设我要为我的网站页面上的列表添加页面按钮(以更改页面),该列表将由过滤器、排序顺序、每页页数等设置控制(由查询字符串控制) )。首先,我需要在 URL 中包含所有当前的查询参数,然后我需要更新页面参数而不篡改其他查询参数。如何从视图/“HTML 助手”生成此 URL?

我当然可以手动操作 URL 字符串,但这会涉及大量工作,并且如果路由发生更改,将很难保持最新状态,必须有更简单的方法吗?就像某种可以在服务端更改的查询字符串集合(例如 ASP.NET Request.QueryString)?

我希望不涉及路线,但无论如何我都会发布迄今为止得到的路线:

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "TreeEditing",
        "{controller}/{action}/{name}/{id}",
        new { controller = "MyCategory", action = "Add", name = string.Empty, id = -1 }
    );

BestRegards

编辑1:可以像这样设置查询参数(在视图中):

<%= url.Action(new {controller="search", action="result", query="Beverages", Page=2})%>

但这只会生成像这样的 URL(使用默认路由):

/search/result?query=Beverages&page=2

如您所见,其余参数将丢失。

我当然可以在此 URL 操作中添加每个已知参数,但是如果添加任何查询参数或更改后,需要做很多工作才能使所有内容保持最新状态。

我已阅读文章ASP.NET MVC 框架(第 2 部分):URL 路由,但是我如何找到问题的答案呢?

I have an ASP.NET MVC site that uses strongly typed views. In my case, a controller action could look like this:

public ActionResult List(MyStrongType data)

When submitting the page (view) the response will generate a URL that looks something like this (yes, I know routing could generate a nicer URL):

http://localhost/Ad/List?F.ShowF=0&ALS.CP=30&ALS.L=0&ALS.OB=0&ALS.ST=0&S=&LS.L1=&LS.L2=&CS.C1=32&CS.C2=34&CS.C3=&ALS.ST=0

If I submit the page again, I can see that the data object in the action is set properly (according to the URL)(default binder).

The problem is: Say that I am to add page buttons (to change the page) for a list on my sitepage, the list will be controlled by settings like filter, sortorder, amount of pages per page and so on (controlled by the querystring). First, I need to include all current query parameters in the URL, and then I need to update the page parameter without tampering with the other query parameters. How can I genereate this URL from the view/"HTML helper"?

I could of course manipulate the URL string manually, but this will involve a lot of work and it will be hard to keep up to date if a route is changed, there must be a easier way? Like some kind of querystring collection that can be altered on service side (like ASP.NET Request.QueryString)?

I would hope to not involve the route, but I post the one I got so far anyway:

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "TreeEditing",
        "{controller}/{action}/{name}/{id}",
        new { controller = "MyCategory", action = "Add", name = string.Empty, id = -1 }
    );

BestRegards

Edit 1: It's possible to set the query parameters like this (in view):

<%= url.Action(new {controller="search", action="result", query="Beverages", Page=2})%>

But this will only generate a URL like this (with the default route):

/search/result?query=Beverages&page=2

The rest of the parameters will be missing as you can see.

I could of course add every known parameter in this URL action, but if any query parameter is added or changed there will be a lot of work to keep everything up to date.

I have read the article ASP.NET MVC Framework (Part 2): URL Routing, but how do I find an answer to my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

君勿笑 2024-10-28 15:09:18

在我看来,您遇到的问题是您希望能够轻松地保留当前请求中的查询字符串值,并将它们呈现到视图中链接的 URL 中。一种解决方案是创建一个 HtmlHelper 方法,该方法返回经过一些更改的现有查询字符串。我为 HtmlHelper 类创建了一个扩展方法,该方法接受一个对象并将其属性名称和值与当前请求中的查询字符串合并,然后返回修改后的查询字符串。它看起来像这样:

public static class StackOverflowExtensions
{
    public static string UpdateCurrentQueryString(this HtmlHelper helper, object parameters)
    {
        var newQueryStringNameValueCollection = new NameValueCollection(HttpContext.Current.Request.QueryString);
        foreach (var propertyInfo in parameters.GetType().GetProperties(BindingFlags.Public))
        {
            newQueryStringNameValueCollection[propertyInfo.Name] = propertyInfo.GetValue(parameters, null).ToString();
        }

        return ToQueryString(newQueryStringNameValueCollection);
    }

    private static string ToQueryString(NameValueCollection nvc)
    {
        return "?" + string.Join("&", Array.ConvertAll(nvc.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))));
    }
}

它将循环遍历当前请求中的查询字符串值,并合并到您传入的对象上定义的属性中。因此您的视图代码可能如下所示:

<a href='/SomeController/SomeAction<%=Html.GetCurrentQueryStringWithReplacements(new {page = "2", parameter2 = "someValue"})%>'>Some Link</a>

这基本上是说“将查询字符串从当前请求,但更改页面和参数2值,或者如果它们不存在则创建它们。”请注意,如果您当前的请求具有“page”查询字符串参数,则此方法将使用您从视图中显式传入的值覆盖当前请求中的值。
在这种情况下,如果您的查询字符串是:

?parameter1=abc&page=1

它将变成:

?parameter1=abc&page=2¶meter2=someValue

编辑:
上面的实现可能不适用于您描述的查询字符串参数名称的字典查找。这是一个实现,它使用字典而不是对象:

    public static string UpdateCurrentQueryString(this HtmlHelper helper, Dictionary<string, string> newParameters)
    {
        var newQueryStringNameValueCollection = new NameValueCollection(HttpContext.Current.Request.QueryString);
        foreach (var parameter in newParameters)
        {
            newQueryStringNameValueCollection[parameter.Key] = parameter.Value;
        }

        return ToQueryString(newQueryStringNameValueCollection);
    }

您的视图将通过对字典进行内联初始化并将其传递给辅助函数来调用该函数,如下所示:

<a href='/SomeController/SomeAction<%=Html.GetCurrentQueryStringWithReplacements(new Dictionary<string,string>() {
{ QuerystringHandler.Instance.KnownQueryParameters[QuaryParameters.PageNr], "2" },
{ QuerystringHandler.Instance.KnownQueryParameters[QuaryParameters.AnotherParam], "1234" }})%>'>Some Link</a>

It sounds to me like the problem you have is that you want to be able to easily persist query string values from the current request and render them into the URLs of links in your view. One solution would be to create an HtmlHelper method that returns the existing query string with some changes. I created an extension method for the HtmlHelper class that takes an object and merges its property names and values with the query string from the current request, and returns the modified querystring. It looks like this:

public static class StackOverflowExtensions
{
    public static string UpdateCurrentQueryString(this HtmlHelper helper, object parameters)
    {
        var newQueryStringNameValueCollection = new NameValueCollection(HttpContext.Current.Request.QueryString);
        foreach (var propertyInfo in parameters.GetType().GetProperties(BindingFlags.Public))
        {
            newQueryStringNameValueCollection[propertyInfo.Name] = propertyInfo.GetValue(parameters, null).ToString();
        }

        return ToQueryString(newQueryStringNameValueCollection);
    }

    private static string ToQueryString(NameValueCollection nvc)
    {
        return "?" + string.Join("&", Array.ConvertAll(nvc.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))));
    }
}

It will loop through the query string values from the current request and merge in the properties defined on the object you passed in. So your view code might look like this:

<a href='/SomeController/SomeAction<%=Html.GetCurrentQueryStringWithReplacements(new {page = "2", parameter2 = "someValue"})%>'>Some Link</a>

This is basically saying "keep the query string from the current request, but change the page and parameter2 values, or create them if they didn't exist." Note that if your current request has a "page" query string parameter, this method will overwrite the value from the current request with the one you explicitly pass in from the view.
In this case, if your querystring was:

?parameter1=abc&page=1

It would become:

?parameter1=abc&page=2¶meter2=someValue

EDIT:
The above implementation will probably not work with the dictionary lookup of querystring parameter names you described. Here is an implementation and that uses a dictionary instead of an object:

    public static string UpdateCurrentQueryString(this HtmlHelper helper, Dictionary<string, string> newParameters)
    {
        var newQueryStringNameValueCollection = new NameValueCollection(HttpContext.Current.Request.QueryString);
        foreach (var parameter in newParameters)
        {
            newQueryStringNameValueCollection[parameter.Key] = parameter.Value;
        }

        return ToQueryString(newQueryStringNameValueCollection);
    }

Your view would call the function by doing an inline initialization of a dictionary and passing it to the helper function like this:

<a href='/SomeController/SomeAction<%=Html.GetCurrentQueryStringWithReplacements(new Dictionary<string,string>() {
{ QuerystringHandler.Instance.KnownQueryParameters[QuaryParameters.PageNr], "2" },
{ QuerystringHandler.Instance.KnownQueryParameters[QuaryParameters.AnotherParam], "1234" }})%>'>Some Link</a>
晨敛清荷 2024-10-28 15:09:18

我做了你所需要的!

我为此创建了一个 HTML 帮助器。该助手采用与普通助手相同的参数。然而,它保留了 URL 中的当前值。我将其作为 URL 帮助程序 制作为 ActionLink 帮助程序

它取代了: Url.Action()

<a href='<%= Html.UrlwParams("TeamStart","Inschrijvingen", new {modID=item.Mod_ID}) %>'  title="Selecteer">
    <img src="<%= Url.Content("~/img/arrow_right.png") %>" alt="Selecteer" width="16" /></a>

并取代了 Html.ActionLink()

<%: Html.ActionLinkwParams("Tekst of url", "Action", new {test="yes"}) %>

这是帮助器:

using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web.Mvc.Html;

namespace MVC2_NASTEST.Helpers {
    public static class ActionLinkwParamsExtensions {
        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs, object htmlAttributes) {

            NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

            RouteValueDictionary r = new RouteValueDictionary();
            foreach (string s in c.AllKeys) {
                r.Add(s, c[s]);
            }

            RouteValueDictionary htmlAtts = new RouteValueDictionary(htmlAttributes);

            RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

            RouteValueDictionary m = RouteValues.MergeRouteValues(r, extra);

            //return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, linktext, action, controller, m, htmlAtts);
            return helper.ActionLink(linktext, action, controller, m, htmlAtts);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action) {
            return ActionLinkwParams(helper, linktext, action, null, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller) {
            return ActionLinkwParams(helper, linktext, action, controller, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, controller, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs, object htmlAttributes) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, htmlAttributes);
        }
    }

    public static class UrlwParamsExtensions {
        public static string UrlwParams(this HtmlHelper helper, string action, string controller, object extraRVs) {
            NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

            RouteValueDictionary r = RouteValues.optionalParamters(c);

            RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

            RouteValueDictionary m = RouteValues.MergeRouteValues(r, extra);

            string s = UrlHelper.GenerateUrl("", action, controller, m, helper.RouteCollection, helper.ViewContext.RequestContext, false);
            return s;
        }

        public static string UrlwParams(this HtmlHelper helper, string action) {
            return UrlwParams(helper, action, null, null);
        }

        public static string UrlwParams(this HtmlHelper helper, string action, string controller) {
            return UrlwParams(helper, action, controller, null);
        }

        public static string UrlwParams(this HtmlHelper helper, string action, object extraRVs) {
            return UrlwParams(helper, action, null, extraRVs);
        }
    }
}

它是如何工作的?

调用与 Html.ActionLink() 相同,因此您可以简单地替换它们。

该方法执行以下操作:

从当前 URL 获取所有可选参数并将它们放入 RouteValueDictionary 中。
它还将 htmlattributes 放入字典中。
然后,它会获取您手动指定的额外路由值,并将它们放入 RouteValueDictionary 中。

接下来的关键是合并 URL 中的内容和手动指定的内容。

这发生在 RouteValues 类中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Collections.Specialized;
using System.Web.Mvc;

namespace MVC2_NASTEST {
    public static class RouteValues {

        public static RouteValueDictionary optionalParamters() {
            return optionalParamters(HttpContext.Current.Request.QueryString);
        }

        public static RouteValueDictionary optionalParamters(NameValueCollection c) {
            RouteValueDictionary r = new RouteValueDictionary();
            foreach (string s in c.AllKeys) {
                r.Add(s, c[s]);
            }
            return r;
        }

        public static RouteValueDictionary MergeRouteValues(this RouteValueDictionary original, RouteValueDictionary newVals) {
            // Create a new dictionary containing implicit and auto-generated values
            RouteValueDictionary merged = new RouteValueDictionary(original);

            foreach (var f in newVals) {
                if (merged.ContainsKey(f.Key)) {
                    merged[f.Key] = f.Value;
                } else {
                    merged.Add(f.Key, f.Value);
                }
            }
            return merged;
        }

        public static RouteValueDictionary MergeRouteValues(this RouteValueDictionary original, object newVals) {
            return MergeRouteValues(original, new RouteValueDictionary(newVals));
        }
    }
}

这一切都非常简单。最后,使用合并的路由值创建 actionlink。此代码还允许您从 URL 中删除值。

示例:

您的 URL 是 localhost.com/controller/action?id=10&foo=bar。如果在该页面中放置此代码,则

 <%: Html.ActionLinkwParams("Tekst of url", "Action", new {test="yes"}) %>

该元素中返回的 URL 将是 localhost.com/controller/action?id=10&foo=bar&test=yes。

如果要删除某个项目,只需将该项目设置为空字符串即可。例如,

 <%: Html.ActionLinkwParams("Tekst of url", "Action", new {test="yes", foo=""}) %>

将返回 中的 URL。元素:localhost.com/controller/action?id=10&test=yes

我猜这就是您所需要的?

如果您还有其他问题,请尽管询问。

额外:

有时,当您将重定向到另一个操作时,您也希望将值保留在操作中。使用我的 RouteValues 类,这可以非常轻松地完成:

 public ActionResult Action(string something, int? somethingelse) {
                    return RedirectToAction("index", routeValues.optionalParamters(Request.QueryString));
 }

如果您仍然想添加一些可选参数,没问题!

 public ActionResult Action(string something, int? somethingelse) {
                    return RedirectToAction("index", routeValues.optionalParamters(Request.QueryString).MergeRouteValues(new{somethingelse=somethingelse}));
 }

我认为这几乎涵盖了您需要的一切。

I did JUST what you need!

I created an HTML helper for this. The helper takes the same parameters as a normal helper. Yet, it keeps the current values from the URL. I made it both for the URL helper as a ActionLink helper.

This replaces the: Url.Action()

<a href='<%= Html.UrlwParams("TeamStart","Inschrijvingen", new {modID=item.Mod_ID}) %>'  title="Selecteer">
    <img src="<%= Url.Content("~/img/arrow_right.png") %>" alt="Selecteer" width="16" /></a>

and this replaces the Html.ActionLink()

<%: Html.ActionLinkwParams("Tekst of url", "Action", new {test="yes"}) %>

Here is the helper:

using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web.Mvc.Html;

namespace MVC2_NASTEST.Helpers {
    public static class ActionLinkwParamsExtensions {
        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs, object htmlAttributes) {

            NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

            RouteValueDictionary r = new RouteValueDictionary();
            foreach (string s in c.AllKeys) {
                r.Add(s, c[s]);
            }

            RouteValueDictionary htmlAtts = new RouteValueDictionary(htmlAttributes);

            RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

            RouteValueDictionary m = RouteValues.MergeRouteValues(r, extra);

            //return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, linktext, action, controller, m, htmlAtts);
            return helper.ActionLink(linktext, action, controller, m, htmlAtts);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action) {
            return ActionLinkwParams(helper, linktext, action, null, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller) {
            return ActionLinkwParams(helper, linktext, action, controller, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, controller, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs, object htmlAttributes) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, htmlAttributes);
        }
    }

    public static class UrlwParamsExtensions {
        public static string UrlwParams(this HtmlHelper helper, string action, string controller, object extraRVs) {
            NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

            RouteValueDictionary r = RouteValues.optionalParamters(c);

            RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

            RouteValueDictionary m = RouteValues.MergeRouteValues(r, extra);

            string s = UrlHelper.GenerateUrl("", action, controller, m, helper.RouteCollection, helper.ViewContext.RequestContext, false);
            return s;
        }

        public static string UrlwParams(this HtmlHelper helper, string action) {
            return UrlwParams(helper, action, null, null);
        }

        public static string UrlwParams(this HtmlHelper helper, string action, string controller) {
            return UrlwParams(helper, action, controller, null);
        }

        public static string UrlwParams(this HtmlHelper helper, string action, object extraRVs) {
            return UrlwParams(helper, action, null, extraRVs);
        }
    }
}

How does it work?

The calls are the same as for the Html.ActionLink() so you simple can replace those.

The method does the following:

It takes all the optional parameters from the current URL and places them in a RouteValueDictionary.
It also places the htmlattributes in a dictionary.
Then it takes the extra routevalues which you specified manually and places those in a RouteValueDictionary as well.

The key then is to merge the ones from the URL and the ones manually specified.

This happens in the RouteValues class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Collections.Specialized;
using System.Web.Mvc;

namespace MVC2_NASTEST {
    public static class RouteValues {

        public static RouteValueDictionary optionalParamters() {
            return optionalParamters(HttpContext.Current.Request.QueryString);
        }

        public static RouteValueDictionary optionalParamters(NameValueCollection c) {
            RouteValueDictionary r = new RouteValueDictionary();
            foreach (string s in c.AllKeys) {
                r.Add(s, c[s]);
            }
            return r;
        }

        public static RouteValueDictionary MergeRouteValues(this RouteValueDictionary original, RouteValueDictionary newVals) {
            // Create a new dictionary containing implicit and auto-generated values
            RouteValueDictionary merged = new RouteValueDictionary(original);

            foreach (var f in newVals) {
                if (merged.ContainsKey(f.Key)) {
                    merged[f.Key] = f.Value;
                } else {
                    merged.Add(f.Key, f.Value);
                }
            }
            return merged;
        }

        public static RouteValueDictionary MergeRouteValues(this RouteValueDictionary original, object newVals) {
            return MergeRouteValues(original, new RouteValueDictionary(newVals));
        }
    }
}

This is all pretty straightforward. In the end, the actionlink is made with the merged routevalues. This code also lets you remove values from the URL.

Examples:

Your URL is localhost.com/controller/action?id=10&foo=bar. If in that page you place this code

 <%: Html.ActionLinkwParams("Tekst of url", "Action", new {test="yes"}) %>

the URL returned in that element will be localhost.com/controller/action?id=10&foo=bar&test=yes.

If you want to remove an item, you just set the item as an empty string. For example,

 <%: Html.ActionLinkwParams("Tekst of url", "Action", new {test="yes", foo=""}) %>

will return the URL in the <a> element: localhost.com/controller/action?id=10&test=yes

I'm guessing this is all you need?

If you have some additional questions, just ask.

Extra:

Sometimes you will want to keep your values inside your action too, when you will redirect to another Action. With my RouteValues class, this can be done very easily:

 public ActionResult Action(string something, int? somethingelse) {
                    return RedirectToAction("index", routeValues.optionalParamters(Request.QueryString));
 }

If you still want to add some optional parameters, no problem!

 public ActionResult Action(string something, int? somethingelse) {
                    return RedirectToAction("index", routeValues.optionalParamters(Request.QueryString).MergeRouteValues(new{somethingelse=somethingelse}));
 }

I think that covers pretty much everything you'll need.

柏林苍穹下 2024-10-28 15:09:18

如果要在视图的链接中设置查询字符串:

Html.ActionLink("LinkName", "Action", "Controller", new { param1 = value1, param2 = value2 }, ...)

如果要在浏览器中设置 URL 回发后,只需在 action 中调用 Route*(如 RouteToAction())并设置所需的参数键/值。

If you want to set the query string in a link of a view:

Html.ActionLink("LinkName", "Action", "Controller", new { param1 = value1, param2 = value2 }, ...)

If you want to set it in the browser URL after a post back, just call Route* in an action like RouteToAction() and set the parameter key/value you want.

倒带 2024-10-28 15:09:18
  • 如果您使用操作 public ActionResult List(MyStrongType data),则需要将所有页面设置(页面索引、排序等)作为“MyStrongType”的参数包含在内,并且数据对象将包含视图的所有信息。

  • 在视图中,如果需要生成URL,使用CallMeLaNN的方法:
    Html.ActionLink("LinkName", "Action", "Controller", new { param1 = Model.value1, param2 = Model.param2, ... });。您需要在这里手动设置所有参数,或者创建一个帮助器来帮助您填写 URL。

  • 您不需要关心当前地址中包含的参数。

  • 您可以路由:
    路线.MapRoute(
    “客户”,
    "{控制器}/{动作}/",
    新的{控制器=“主页”,操作=“索引”}
    );
    将所有参数生成为查询字符串。

  • If you use the action public ActionResult List(MyStrongType data), you need to include all page settings (page index, ordering,...) as parameters to 'MyStrongType', and the data object will contain all infomation for the view.

  • In the view, if you need to generate a URL, using the approach of CallMeLaNN:
    Html.ActionLink("LinkName", "Action", "Controller", new { param1 = Model.value1, param2 = Model.param2, ... });. You need to manually set all the parameters here or create a helper to help you fill the URL.

  • You don't need to care about current parameters included in the address.

  • You can route:
    routes.MapRoute(
    "custome",
    "{controller}/{action}/",
    new { controller = "Home", action = "Index"}
    );
    to generate all the parameters as a query string.

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