特定の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
}