我是新来web开发和我的ASP.Net MVC-5项目,我做了一个接受get请求的动作。我想要在控制台桌面应用程序上点击我的控制器的动作。以下是我的新手尝试。请指导我。创建控制台应用程序客户端来访问控制器动作ASP.NET MVC 5
在我的控制,我添加以下措施
public class SecurityController : BaseController
{
[HttpGet]
public ActionResult CacheClear(TestMV viewModel)
{
// SOME CODE
return View();
}
}
我TestMV
public class TestMV
{
public int locationId { get; set; }
}
现在,当我去到浏览器并输入http://localhost:5271/Security/CacheClear/?locationId=28463我能够命中断点和我我能够捕捉到价值。
问题1: 是吗?或者我需要为我的控制台应用程序打一些花哨的东西来打这个Action方法。我是否需要创建一些Web API或其他?或上面的代码将正常工作?
问题2:如何通过桌面应用访问Controller操作。
我GOOGLE了写下面的代码:
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();
}
}
}
有人能指导我,如果我在正确的方向我要去。我需要在代码中做出什么改变才能实现结果。另外,我在await client.GetAsync
附近变得越来越弯曲。 “等待方法只能在同步方法中使用,请帮助我。