この記事では、既存のクラシックASP.NETアプリケーションにRESTful APIを迅速に実装する方法を説明しています。
MVCライブラリの機能を最大限に活用する方法。
使用するツール1. System.Web.Routing。
mysite.ru/rest/client/0などのリンクを受信するための
RouteTableおよび
IRouteHandler2. System.Web.Mvc。
DefaultModelBinderは、リクエストからモデルへのデータの書き込みを回避します
3. System.Web。 受信したリクエストをCRUD操作の1つに変換する
IHttpHandler動作原理RouteTableにRouteを追加し、テンプレートに従って、要求を必要なHttpHandlerにリダイレクトします。
CRUD操作用とパラメーター検索操作用の2つのルートを登録します。
HttpHandlerは、要求メソッドと渡されたパラメーターによって目的の操作を選択します。
取得リクエストであり、クエリパラメータが存在する場合、パラメータによる検索操作が選択されます。
これが書き込み操作(作成、更新、削除)の場合、DefaultModelBinderの子孫を使用して目的のモデルを作成またはロードし、要求から受け取ったデータがそれに適用されます。
読み取り操作中に、idパラメーターが渡された場合、1つのモデルが選択され、idが渡されなかった場合、モデルのコレクション全体が返されます。
最後のステップは、モデルをJSONオブジェクトに変換することです。
応答では、キャッシュが30秒間構成されます。
コードが乱雑にならないように、構成を実装しませんでした。
ソリューションを構成するとき、2つの問題が発生する可能性があります。
1. 404エラー-IISのファイル存在チェックを無効にすることで処理されます
(web.config設定の
ここまたは以下を参照)
2.セッションのオブジェクトが存在しない-セッションモジュールを再登録することで処理されます
(web.config設定の
ここまたは以下を参照)
アプリケーションのソースは
ここからダウンロードでき
ますクライアントモデルのサービス実装例クラスClientRestHttpHandlerpublic class ClientRestHttpHandler : RestHttpHandler<Client, ClientModelBinder> { protected override IEnumerable<Client> GetAll() { return ClientService.GetAll(); } protected override Client GetBy(int id) { return ClientService.GetById(id); } protected override IEnumerable<Client> GetBy(NameValueCollection query) { var result = ClientService.GetAll(); var contains = query["contains"]; if (contains != null) { result = from item in result where item.FirstName.Contains(contains) || item.LastName.Contains(contains) select item; } return result; } protected override void Create(Client entity) { ClientService.Create(entity); } protected override void Update(Client entity) { ClientService.Update(entity); } protected override void Delete(Client entity) { ClientService.Delete(entity); } protected override object ToJson(Client entity) { return new { entity.Id, entity.FirstName, entity.LastName }; } }
クラスClientModelBinder public class ClientModelBinder : DefaultModelBinder { protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, System.Type modelType) { var value = bindingContext.ValueProvider.GetValue("id"); if (value == null) return ClientService.New(); var result = (int)value.ConvertTo(typeof(int)); return ClientService.GetById(result); } }
Global.asaxの名前 void Application_Start(object sender, EventArgs e) { RestRouteHandler<ClientRestHttpHandler>.Register("client", "clients"); }
web.configの構成 <configuration> <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <remove name="Session" /> <add name="Session" type="System.Web.SessionState.SessionStateModule"/> </modules> <handlers> <add name="WildCard" path="*" verb="*" resourceType="Unspecified" /> </handlers> </system.webServer> </configuration>
それだけです。クライアントモデルは、当社のWebサイトからREST APIを介して利用できます。
基本クラスRestHttpHandlerおよびRestRouteHandlerのソース public abstract class RestHttpHandler : IHttpHandler, IReadOnlySessionState { public const string ParamKeyId = "id"; public const string ParamKeyQuery = "query";
public abstract class RestHttpHandler<T, TBinder> : RestHttpHandler where T : class where TBinder : DefaultModelBinder, new() {
public class RestRouteHandler<T> : IRouteHandler where T : RestHttpHandler, new() { IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext) { return new T() { RouteData = requestContext.RouteData }; } public static void Register(string name, string pluralName) { RouteTable.Routes.Add( name, new Route( string.Format( "rest/{0}/{{{1}}}", name, RestHttpHandler.ParamKeyId ), new RestRouteHandler<T>() ) { Defaults = new RouteValueDictionary { { RestHttpHandler.ParamKeyId, null } }, Constraints = new RouteValueDictionary { { RestHttpHandler.ParamKeyId, "\\d*" } } } ); RouteTable.Routes.Add( pluralName, new Route( string.Format( "rest/{0}/{{*{1}}}", pluralName, RestHttpHandler.ParamKeyQuery ), new RestRouteHandler<T>()) { Defaults = new RouteValueDictionary { { RestHttpHandler.ParamKeyQuery, "" } } } ); } }
私の経験がお役に立てば幸いです。