c# - MVC Controller to API Controller -
i have mvc controller written. now, have code same action methods of mvc controller new api controller.
what changes needed put method in api controller?
for eg. mvc controller code
[httpget] [eauthorize(xyz)] public actionresult trans() { var user = (user)websession.currentuser; var mtrans = mtranslmgr.initializemtrans(user.currentselectedentity.value); mtranslmgr.accnum = tcontroller.getaccountnumberbasedonprivilage(x,y); return view(mtranslmgr); } if have implement same method in api controller, changes have implement. methods should return json.
you cannot convert mvc controller api controller. both types devires different base classes. in asp.net mvc controller have derive controller class, while in asp.net web api controller base class apicontroller. in both cases, inherit custom controller base, 1 must inherit original base controller provided plataform.
in asp.net web api, result format provided content-type provided on http header. so, client application must provided application/json on content-type attribute , asp.net web api serialize in format you. sample:
public httpresponsemessage get(int id) { var user = (user)websession.currentuser; var mtrans = mtranslmgr.initializemtrans(user.currentselectedentity.value); mtranslmgr.accnum = tcontroller.getaccountnumberbasedonprivilage(x,y); // return ok model serialized on response body request.createresponse(httpstatus.ok, mtranslmgr); } i not sure have on websession.currentuser, if use key on httpcontext.session[key] cannot because asp.net stateless , not have sessions.
for eauthorize have implement authentication in http header. can using basic authentication, default behaviour of rest applications. recommend read article basic authentication in asp.net web api , see in details how it.
your question broad , maybe other users can close it. think try implement , ask posting code of you've tried.
Comments
Post a Comment