最初のものに対してできることは次のとおりです。サイトの Global.asax.cs ファイルを開き、これを標準の MVC キャッチオール ルート ( route を使用するもの)のRegisterRoutes
前に配置します。"{controller}/{action}/{id}"
routes.MapRoute("AddBuilding", "configuration/building/add",
new { controller = "Configuration", action = "AddBuilding" });
他のものは同じですが、名前 (最初のパラメーター) とアクションが異なります。ルートを編集しますが、{id}
ルート プレースホルダーとルート パラメーターが含まれます (ただし、オプションではありません - MVC の既定のルートとは異なります)。
routes.MapRoute("EditBuilding", "configuration/building/edit/{id}",
new { controller = "Configuration", action = "EditBuilding" });
id
ルート外のデフォルトのままにすることで、必須にします。/Building/Edit
Urlは論理的に何にもマップされていないと推測しているため、これを想定しています。
サイド ノードとして- URL に動詞を含めることは、実際には REST 方法論と一致していませんが、長い道のりでそれを行ったのはあなたが最初ではありません (私自身もその中に含まれています)。とは言っても、それを守ろうとすると、通常は生活がずっと楽になります。コントローラーがクリーンになり、ルート テーブルもきれいになり、サイトの URL スペースがはるかに小さくなり、階層がより明確になるからです。この最後のポイントは、開発時にサイトをズームするのに便利ですが、さらに重要なことは、SEO にとって非常に重要なことです。
だから(私はこのコードにかなりのコメントを付けました。うまくいけば、知識のナゲットを提供するのに十分です!):
public class ConfigurationController{
////HTTP GET /Buildings
/// DISPLAYS BUILDINGS
public ActionResult Buildings(){
//get model and return view that shows all buildings with perhaps a
//partial in that for creating a new one (or you can use another action)
//The HTML form on that view will POST to the URL handled by the method below.
}
////HTTP POST /Buildings
/// CREATES A NEW BUILDING
//use ActionName here to make this and the method above accessible through
//the same URL
[ActionName("Buildings")]
[HttpPost]
public ActionResult CreateBuilding(BuildingModel model){
//validate the model, create the object and return the same
//view as the Buildings() method above (after re-loading all the
//buildings. Or, you can issue a redirect, effectively transferring
//control back to the method above.
}
////HTTP GET /Configuration/Building/id
///DISPLAYS A BUILDING
public ActionResult Building(int id){
//get building and return view, which also contains Edit functionality
}
////HTTP POST /Configuration/Building/id
///EDITS A BUILDING
[HttpPost]
public ActionResult Building(int id, BuildingModel model){
//very similar to the CreateBuilding method - and again you might
//either simply return a building view at the end, or redirect
//to the method above.
//Note that we don't need [ActionName] here because this and the
//get method can have the same method names, because they are overloads
//i.e. since they have different method signatures we can call them the same
//thing in code.
}
}
簡潔にするために、グループの内容は省略しました。そこから方法を確認できることを願っています。
これが整っていれば、Global.asax.cs に必要なルートは最大で 2 つだけです。ただし、順序は重要になると思います。
//handles both GET and POST to this URL, i.e. our view & edit operations
routes.MapRoute("IndividualBuilding", "/configuration/buildings/{id}",
new { controller = "Configuration", action = "Building" });
routes.MapRoute("Buildings", "/configuration/buildings",
new { controller = "Configuration", action = "Buildings" });
現在、HTTP 動詞を使用して特定のリクエストで何をしようとしているのかを示しており、URL はより「論理的」になりました。
別のリファクタリング
「賢く」なりたい場合は、建物とグループの両方を 2 つのルートの下にまとめることができます
//handles both GET and POST to this URL, i.e. our view & edit operations
routes.MapRoute("Individual", "/configuration/{controller}/{id}",
new { controller = "Configuration", action = "List" });
//again, handles GET and POST
routes.MapRoute("Collection", "/configuration/{controller}",
new { controller = "Configuration", action = "Single" });
上に示したように、建物とグループの両方のコントローラーを実行しますが、 ( 2 番目のメソッドの属性をBuildings
思い出して)とに置き換えます。ActionName
List
Building
Single
最後に考慮すべきことは、デフォルトの MVC ルートのため、次のことです。
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Default", action="Home", id = UrlParameter.Optional });
2 つのコントローラーはどちらも、たとえば/Buildings/Single/1
or経由でルーティングできます。/Groups
これは些細な問題ですが (偽のコンテンツは優れた SEO ではありません)、人々があなたのサイトを盗聴するために使用できるものになる可能性があります。
この他の URL 形式を絶対に防止したい場合。デフォルトルートを取り出すことができます。つまり、すでに機能している可能性のある他のものを明示的にルーティングする必要があります (大きな問題ではありません) 。
または、ルート名に明示的な[ActionName]
属性を使用して、ルート名に IIS では許可されない文字を使用することもできます。":Single"
":List"