3

私はこのファイルを持っています:

<fru>
 <url action="product" controler="Home" params="{id=123}" >this_is_the_an_arbitrary_url_of_a_product.html</url>
 <url action="customer" controler="Home" params="{id=445}" >this_is_the_an_arbitrary_url_of_a_customer.html</url>
... other urls ...
</fru>

パラメータを使用してリクエストを適切なアクションにマップするために、このファイルまたはこのオブジェクト構造を MVCHandler にバインドすることを探しています。URL が構造規則を尊重しないことは、url me にとって重要です。

これを行う方法はありますか?

4

1 に答える 1

2

特定のURL構造を使用せずに、MVCプロジェクトに完全に任意のルートを設定できます。

Global.asax.csの場合:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        "",
        "superweirdurlwithnostructure.whatever",
        new { controller = "Home", action = "Products", id = 500 }
    );
}

通常のルートをマッピングする前に、必ずこれを行ってください。これらの特定のものを最初に実行する必要があります。そうしないと、デフォルトルートエントリによって要求が通過しなくなります。

表示したXMLファイルに基づいて作成する場合は、次のような簡単なことを行います。

XDocument routes = XDocument.Load("path/to/route-file.xml");
foreach (var route in routes.Descendants("url"))
{
    //pull out info from each url entry and run the routes.MapRoute(...) command
}

もちろん、XMLファイル自体を何らかの方法でプロジェクトに埋め込むこともできます。それはあなた次第です。

編集:

パラメータの処理については、コマンドを使用して任意のパラメータを簡単に送信できますroutes.MapRoute(...)。次のように追加するだけです。

routes.MapRoute(
    "",
    "my/route/test",
    new { controller = "Home", action = "Index",
          id = "500", value = "hello world" } // <- you can add anything you want
);

次に、アクションでは、次のように実行します。

//note that the argument names match the parameters you listed in the route:
public ActionResult Index(int id, string value)
{
    //do stuff
}
于 2012-10-30T09:32:11.770 に答える