0

構成できるオブジェクトが多数あるアーキテクチャがあります。必要な URL の例は次のとおりです。

/configuration/building/add
/configuration/building/edit/1
/configuration/group/add
/configuration/group/edit/1

私は構成コントローラーを持っていますが、どのようにインターセプトまたは対処しますかbuilding/add...building/edit/1もしそうなら、単純に関数をAddBuilding追加できます。AddBuilding()configuration/building/edit/

4

4 に答える 4

2

最初のものに対してできることは次のとおりです。サイトの 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/EditUrlは論理的に何にもマップされていないと推測しているため、これを想定しています。

サイド ノードとして- 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思い出して)とに置き換えます。ActionNameListBuildingSingle

最後に考慮すべきことは、デフォルトの MVC ルートのため、次のことです。

routes.MapRoute("Default", "{controller}/{action}/{id}", 
  new { controller = "Default", action="Home", id = UrlParameter.Optional });

2 つのコントローラーはどちらも、たとえば/Buildings/Single/1or経由でルーティングできます。/Groupsこれは些細な問題ですが (偽のコンテンツは優れた SEO ではありません)、人々があなたのサイトを盗聴するために使用できるものになる可能性があります。

この他の URL 形式を絶対に防止したい場合。デフォルトルートを取り出すことができます。つまり、すでに機能している可能性のある他のものを明示的にルーティングする必要があります (大きな問題ではありません)

または、ルート名に明示的な[ActionName]属性を使用して、ルート名に IIS では許可されない文字を使用することもできます。":Single"":List"

于 2012-07-27T12:59:07.040 に答える
1

したがって、最初に、AddBuilding()示唆したように呼び出されるコントローラーアクションを作成できます。

次にGlobal.asax、メソッドのファイルで、次のRegisterRoutesようにルートを追加できます。

routes.MapRoute(
   "AddBuilding", // Route name
   "configuration/building/add", // URL with parameters
   new { controller = "Configuration", action = "AddBuilding" }
);

ただし、デフォルトのルート マッピングにより、「/configuration/addbuilding」を使用してページにアクセスできる可能性が高いことに注意してください。

ID編集すると、これに値をマップすることを期待して似たものになります。

routes.MapRoute(
   "EditBuilding", // Route name
   "configuration/building/edit/{id}", // URL with parameters
   new { controller = "Configuration", action = "AddBuilding", id = UrlParameter.Optional }
);

MapRouteこのコードをデフォルトのセットアップで追加して、優先されないようにする必要があると思います

于 2012-07-27T13:04:13.117 に答える
0

別のアプローチは、構成 MVC を作成し、その中areaに建物とグループcontrollerを含めることAreaです。

于 2012-07-27T13:06:08.700 に答える
0

ASP.NET MVC 5 の属性ルーティングによってそれを行うことができます。次のようなものです。

// eg: /reviews
[Route(“reviews”)]
public ActionResult Index() { … }
// eg: /reviews/5
[Route(“reviews/{reviewId}”)]
public ActionResult Show(int reviewId) { … }
// eg: /reviews/5/edit
[Route(“reviews/{reviewId}/edit”)]
public ActionResult Edit(int reviewId) { … }

同じコントローラーに複数のルートを追加することもできます。詳細はこちらをご確認ください

于 2016-02-29T19:53:34.710 に答える