我有一個網站,顯示-Default.aspx,現在我加了的WebAPI CONTROLER類項目。該頁面,我怎樣才能得到它運行,也就是說,如果我只是創建一個Post請求與我的URL不起作用。下面是我的webapi控制器類,我如何將它集成到我當前的網站 - 所以我可以發佈請求並獲取返回字符串。怎樣的WebAPI添加到ASPX頁面
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)
{
}
}
如果我做一個POST請求「www.xyz.in」我得到整個網頁在我的收益流,而不是,我在我的帖子以上功能返回字符串「Test」。在我的應用程序
客戶端調用 - 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();
下面是得到了添加到我的web.config(由Visual Studio)文件,當我加入的WebAPI類,我的項目內容。
<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>
你串回來在Ajax請求的數據..請確保你包括你正在做的崗位價值,也可能不會映射propertly –
一個基本的疑問,以確認是否是正確地做事,在客戶端應用程序要打發POST請求我的默認網址我網站,我應該使用/ api/Valcontrol來執行Post事件嗎? – user1066231
是的,你很可能做出正確的發佈請求,你只是沒有你的控制器appropiately命名 –