如何将webapi添加到aspx网页

时间:2017-09-03 16:37:09

标签: c# asp.net asp.net-web-api asp.net-web-api-routing

我有一个显示页面-Default.aspx的网站,现在我在项目中添加了一个webapi控制器类。如何让它运行。即,如果我只是用我的URL创建一个Post请求它是不工作下面是我的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)
    {
    }
}

如果我向&#34; www.xyz.in&#34;我在我的返回流中获取整个网页而不是字符串&#34;测试&#34;我在上面的Post函数中返回。

我的应用程序中的客户端调用 - 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();

当我将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>

1 个答案:

答案 0 :(得分:0)

此sw水不起作用Valcontrol不是控制器的有效名称。控制器需要具有后缀控制器才能正确映射。

因此您需要将名称更改为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)
    {
    }
}

然后像你这样提出你的帖子请求:

POST  api/val?value=Test

并将标题更改为Content-Type:application / json