c# - Create console app client to access the controller action ASP.NET MVC 5 -
i new web development , in asp.net mvc-5 project , have made 1 action accepts request. want on console desktop application hit action of controller. below novice attempt. please guide me.
in controller added below action
public class securitycontroller : basecontroller { [httpget] public actionresult cacheclear(testmv viewmodel) { // code return view(); } }
my testmv
public class testmv { public int locationid { get; set; } }
now, when go browser , type http://localhost:5271/security/cacheclear/?locationid=28463 able hit breakpoint , able capture value.
question 1: it? or need fancy stuff me console app hit action method. need create web api or something. or above code work fine?
question 2: how access controller action via desktop app.
i googled wrote below code:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace consoleapplication2 { class program { static void main(string[] args) { system.net.http.httpclient client = new system.net.http.httpclient(); client.baseaddress = new uri("http://localhost:5271/"); client.defaultrequestheaders.accept.add(new system.net.http.headers.mediatypewithqualityheadervalue("application/json")); var resp2 = await client.getasync("security/cacheclear/?locationid=28463"); resp2.ensuresuccessstatuscode(); } } }
can guide me if going in right direction. changes need make in code acheive result. also, getting squiggly near await client.getasync
. "the await method can used within sync method. please me.
mvc controllers have define behavior mvc web apps. think need it's create asp.net web api project use controllers extends o inherits base class apicontroller , allows more regular mvc app controllers. 2 links help:
getting started asp.net web api
calling web api .net client
hope helps!
Comments
Post a Comment