[英]how to add webapi to aspx web page
I have a website that display the page -Default.aspx, Now I added a webapi controler class to the project., how can I get it running., ie, if I just create a Post request with my URL it is not working. 我有一个显示页面-Default.aspx的网站,现在我在项目中添加了一个webapi控制器类。如何使其运行。即,如果我仅使用URL创建一个Post请求,则该请求不起作用。 below is my webapi controller class, how can I integrate this to my current website- so I can do post requests and get the return string.
以下是我的webapi控制器类,如何将其集成到当前网站中-这样我就可以发布请求并获取返回字符串。
public class Valcontrol : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public String Post([FromBody]string value)
{
return "Test";
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
If I do a Post request to "www.xyz.in" am getting the whole webpage in my return stream instead of the string "Test" that I am returning in my Post function above. 如果我对“ www.xyz.in”执行Post请求,则会在返回流中获得整个网页,而不是在上面的Post函数中返回的字符串“ Test”。
Client call in my App - Java code: 我的应用程序中的客户端调用-Java代码:
URL url = new URL("http://www.xyz.in/");
// get the payuConfig first
String postParam = "Dev";
byte[] postParamsByte = postParam.getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postParamsByte.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postParamsByte);
InputStream responseInputStream = conn.getInputStream();
StringBuffer responseStringBuffer = new StringBuffer();
byte[] byteContainer = new byte[1024];
for (int i; (i = responseInputStream.read(byteContainer)) != -1; ) {
responseStringBuffer.append(new String(byteContainer, 0, i));
}
String strResponse = responseStringBuffer.toString();
Below is the content that got added to my web.config(by visual studio) file when I added webapi class to my project. 以下是当我将webapi类添加到项目中时添加到我的web.config(由Visual Studio)文件中的内容。
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit"/>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"/>
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0"/>
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
This swill not work Valcontrol is not a valid name for a controller. 这将不起作用Valcontrol不是控制器的有效名称。 Controllers need to have the Suffix Controller in order for them to map correctly.
控制器需要具有后缀控制器才能正确映射。
So you need to change the name to ValController, or ValControlController. 因此,您需要将名称更改为ValController或ValControlController。
[Route("api/[controller]")]
public class ValController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public String Post([FromBody]string value)
{
return "Test";
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
then make your post request like so: 然后像这样发出您的帖子请求:
POST api/val?value=Test
and change your headers to Content-Type:application/json 并将标头更改为Content-Type:application / json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.