[英]Pass long HTML string as parameter to Web API from MVC Controller
我正在开发Web API以及MVC应用程序。 现在,我的要求是将HTML作为字符串从MVC控制器传递到Web API方法。
我在MVC应用程序中使用了以下代码来调用Web服务:
using (var client = new HttpClient())
{
byte[] Result = null;
client.BaseAddress = new Uri("http://localhost:1004/");
HttpResponseMessage Res = await client.GetAsync(string.Format("api/Method?htmlString={0}", htmlString));
if (Res.IsSuccessStatusCode)
{
Result = Res.Content.ReadAsByteArrayAsync().Result;
}
if (Result != null)
{
//Work with byte array
}
}
以下是我在Web API中方法的声明
[HttpGet]
[Route("api/Method")]
public HttpResponseMessage Method(string htmlString)
{
try
{
//work with htmlString
}
catch (Exception ex)
{
HttpError err = new HttpError(ex.ToString());
return Request.CreateResponse(HttpStatusCode.NotFound, err);
}
}
但是上面的代码不起作用,我得到一个错误
然后,我实现了日志记录,发现Web API中的参数htmlString为空,在传递HTML时不应发生
我什至尝试在MVC中对HTML进行编码,然后将其传递给Web API,但这也不起作用。
那么谁能告诉我在这种情况下我该怎么办?
提前致谢
[编辑]我试图通过MVC应用程序传递简单的HTML,如下所示,我在Web API中获得了价值
<html>
<body>
Hello World.
</body>
</html>
但是它不适用于我的长HTML
我认为您需要增加允许的URL长度或查询字符串的大小:
<httpRuntime maxUrlLength="260" maxQueryStringLength="2048" />
要允许更长或更短的路径(URL中不包含协议,服务器名称和查询字符串的部分),请修改maxUrlLength属性。 要允许更长或更短的查询字符串,请修改maxQueryStringLength属性的值。
参考:
好吧,我认为最好的办法是,如果您尝试将某些内容传递给Web api,则应该使用POST请求在HTTP请求正文中发送HTML,因此您不必担心内容大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.