簡體   English   中英

在MVC 4中通過路由更改URL參數

[英]Changing URL parameters with routing in MVC 4

我的一些API函數允許使用稱為“ attribute”和“ attributeDelimiter”(以單數形式)的參數,這意味着預期的URL的格式為

SomeController / SomeAction?aaa = bbb&attribute = ccc&attributeDelimiter = ddd。

我也希望在這些參數名稱中也支持復數形式-'attributes'和'attributesDelimiter'。

  1. 有沒有辦法在RouteConfig中重寫URL? (將復數名稱更改為單數)
  2. 如果這是不可能的,或者不是最佳實踐,那么進行這種重寫的最佳方法是什么?

MVC不使用路由查詢字符串值。 查詢字符串值由提供給操作方法值提供 因此,要解決此問題,您只需要一個自定義值提供程序即可處理單數或復數的情況。

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Web.Mvc;

public class SingularOrPluralQueryStringValueProviderFactory : ValueProviderFactory
{
    private readonly string singularKey;
    private readonly string pluralKey;

    public SingularOrPluralQueryStringValueProviderFactory(string singularKey, string pluralKey)
    {
        if (string.IsNullOrEmpty(singularKey))
            throw new ArgumentNullException("singularKey");
        if (string.IsNullOrEmpty(pluralKey))
            throw new ArgumentNullException("pluralKey");

        this.singularKey = singularKey;
        this.pluralKey = pluralKey;
    }

    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {
        return new SingularOrPluralQueryStringValueProvider(this.singularKey, this.pluralKey, controllerContext.HttpContext.Request.QueryString);
    }
}

public class SingularOrPluralQueryStringValueProvider : IValueProvider
{
    private readonly string singularKey;
    private readonly string pluralKey;
    private readonly NameValueCollection queryString;


    public SingularOrPluralQueryStringValueProvider(string singularKey, string pluralKey, NameValueCollection queryString)
    {
        if (string.IsNullOrEmpty(singularKey))
            throw new ArgumentNullException("singularKey");
        if (string.IsNullOrEmpty(pluralKey))
            throw new ArgumentNullException("pluralKey");

        this.singularKey = singularKey;
        this.pluralKey = pluralKey;
        this.queryString = queryString;
    }

    public bool ContainsPrefix(string prefix)
    {
        return this.GetSingularOrPluralValue(prefix) != null;
    }

    public ValueProviderResult GetValue(string key)
    {
        var value = this.GetSingularOrPluralValue(key);
        return (value != null) ? 
            new ValueProviderResult(value, value.ToString(), CultureInfo.InvariantCulture) : 
            null;
    }

    private bool IsKeyMatch(string key)
    {
        return (this.singularKey.Equals(key, StringComparison.OrdinalIgnoreCase) ||
            this.pluralKey.Equals(key, StringComparison.OrdinalIgnoreCase));
    }

    private string GetSingularOrPluralValue(string key)
    {
        if (this.IsKeyMatch(key))
        {
            return this.queryString[this.singularKey] ?? this.queryString[this.pluralKey];
        }
        return null;
    }
}

用法

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        // Insert our value provider factories at the beginning of the list 
        // so they override the default QueryStringValueProviderFactory
        ValueProviderFactories.Factories.Insert(
            0, new SingularOrPluralQueryStringValueProviderFactory("attribute", "attributes"));
        ValueProviderFactories.Factories.Insert(
            1, new SingularOrPluralQueryStringValueProviderFactory("attributeDelimiter", "attributesDelimiter"));
    }
}

現在,在您的操作方法中,甚至在模型的屬性中,無論在查詢字符串中將值指定為單數還是復數,都將填充這些值。 如果查詢字符串中包括單數和復數,則單數優先。

public ActionResult Index(string attribute, string attributeDelimiter)
{
    return View();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM