asp.net - Can I configure a controller to reject all Post methods? -
we have few controllers expect process requests. when post arrives returns 500 , rather return 405 (method not allowed). there way set routes on controller return 405 when post received? of controllers need accept post cant in iis config (i.e configure reject verbs). information platform azure web app.
i have solution works, has disadvantage of having added every route, seems cumbersome.
[route("example/route/{date:datetime}")] [acceptverbs("get", "post")] public periods getexampleroute(datetime date) { if (request.method.method.equals("post")) { throw new httpresponseexception(httpstatuscode.methodnotallowed); } ... processing ... }
you could mvc actionfilter
(similarly web api, system.web.http
):
public class restrictverbsattribute : actionfilterattribute { private string protocol { get; } public restrictverbsattribute(string verb) { protocol = verb; } public override void onactionexecuting(actionexecutingcontext filtercontext) { var request = filtercontext.requestcontext.httpcontext; var result = request.request.httpmethod.equals(protocol, stringcomparison.ordinalignorecase); if (!result) { filtercontext.result = new httpstatuscoderesult(httpstatuscode.methodnotallowed); //405 } } }
which can use @ controller
or action
level
[restrictverbs("get")] public class verbscontroller : controller { public actionresult index() { return view(); } public actionresult about() { return view(); } }
posting action in controller:
hth...
Comments
Post a Comment