-1

私の目標は、CompanyController のすべてのメソッドが、RouteValueDictionary を介して明示的に渡す必要なく、同じ「id」パラメーターを共有することです。これは、煩雑でエラーが発生しやすいためです。

たとえば、http://FooBar.com/Company/Index/4782 というページいて、同じコントローラーの「Profile」メソッドへのリンクを生成したい場合、@Html.ActionLink("Go to profile"、"Profile")、 http: //FooBar.com/Company/Profile/4782が生成され、そのようなリンクが必要になるたびに新しい {id=id} を指定する必要はありません。

おすすめの方法はありますか?

4

2 に答える 2

0

内部のActionlinkの実装System.Web.Mvc.Html.LinkExtensions (System.Web.Mvc, Version=4.0.0.0)は次のとおりです...

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
  if (string.IsNullOrEmpty(linkText))
    throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
  else
    return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, (string) null, actionName, controllerName, routeValues, htmlAttributes));
}

さまざまなパラメータの組み合わせによるユーティリティのオーバーロードがいくつかあります。

したがって、リンクを簡単に(テストされていない状態で)生成するための拡張メソッドを追加できるはずです...

public static MvcHtmlString InternalActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
  if (string.IsNullOrEmpty(linkText))
    throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
  else
    RouteValueDictionary.Values.Add("id", htmlHelper.ViewContext.RequestContext.RouteData)
    return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, (string) null, actionName, controllerName, routeValues, htmlAttributes));
}

public static MvcHtmlString InternalActionLink(this HtmlHelper htmlHelper, string linkText, string actionName)
{
  return this.InternalActionLink(htmlHelper, linkText, actionName, (string) null, new RouteValueDictionary(), (IDictionary<string, object>) new RouteValueDictionary());
}

明らかに、必要な機能を正確に提供するには、いくつかの単純なラッパーオーバーロードを追加する必要があります。

次に使用するだけでhtml.InternalActionLink("Title", "Action")、URLを生成するときに正しいIDがルートディクショナリに自動的に挿入されます。

ちなみに、次のActionLink()オーバーロードはフレームワークによって提供されます-実装すればするほど、柔軟性が高まります...

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues)
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName)
于 2012-11-21T16:28:13.393 に答える
0

デフォルトルートのパラメータの順序を変更することで、それを受け入れることができれば、望ましい結果が得られます。

したがって、あなたが持っている唯一のルートがこれである場合(デフォルトがないことに注意してください):

        routes.MapRoute(
            name: "NoDefault",
            url: "{controller}/{action}/{id}/"
        );

次に、http://foobar.com/Company/Index/4782ページから呼び出します

@Html.ActionLink("Manage Products", "ManageProducts", "Company", null, null) 

この間違ったリンクが表示されます(現在のページのようです): http: //foobar.com/Company/Index/4782/したがって、機能しません。ただし、次のようにルート内のパラメータの順序を逆にすると、次のようになります。

        routes.MapRoute(
            name: "NoDefault",
            url: "{controller}/{id}/{action}"
        );

同じページhttp://foobar.com/Company/4782/Index/から開始すると、同じActionLink()呼び出しが正常に機能し 、正しいリンクが生成されます:http : //foobar.com/Company/4782/ManageProducts

于 2012-11-21T00:54:07.603 に答える