我正在使用Asp.Net Core作为Rest Api服务。我需要访问ActionFilter中的请求和响应。实际上,我在OnActionExcecuted中找到了请求,但无法读取响应结果。
我正在尝试返回值,如下所示:
[HttpGet]
[ProducesResponseType(typeof(ResponseType), (int)HttpStatusCode.OK)]
[Route("[action]")]
public async Task<IActionResult> Get(CancellationToken cancellationToken)
{
var model = await _responseServices.Get(cancellationToken);
return Ok(model);
}
Run Code Online (Sandbox Code Playgroud)
并在ActionFilter的OnExcecuted方法中如下所示:
_request = context.HttpContext.Request.ReadAsString().Result;
_response = context.HttpContext.Response.ReadAsString().Result; //?
Run Code Online (Sandbox Code Playgroud)
我试图在ReadAsString中将响应作为扩展方法如下:
public static async Task<string> ReadAsString(this HttpResponse response)
{
var initialBody = response.Body;
var buffer = new byte[Convert.ToInt32(response.ContentLength)];
await response.Body.ReadAsync(buffer, 0, buffer.Length);
var body = Encoding.UTF8.GetString(buffer);
response.Body = initialBody;
return body;
}
Run Code Online (Sandbox Code Playgroud)
但是,没有结果!
如何在OnActionExcecuted中获得响应?
谢谢大家花时间尝试帮助解释
我有Asp.Net MVC应用程序,我使用了structuremap,我使用自定义ThrottleAttribute ActionFilter.遵循asp.net mvc中的速率限制
public enum TimeUnit
{
Minute = 60,
Hour = 3600,
Day = 86400
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class ThrottleAttribute : ActionFilterAttribute
{
public TimeUnit Time { get; set; }
public int Count { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var seconds = Convert.ToInt32(Time);
var key = string.Join(
"-",
seconds,
filterContext.HttpContext.Request.HttpMethod,
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
filterContext.ActionDescriptor.ActionName,
filterContext.HttpContext.Request.UserHostAddress
);
// increment the cache value
var cnt = 1;
if (HttpRuntime.Cache[key] != null)
{
cnt = …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Asp.net Core API 并设置如下服务:
services
.Configure<AppOptions>(_configuration.GetSection("app"))
.AddMvcCore(options =>
{
options.RespectBrowserAcceptHeader = true;
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
})
.AddFormatterMappings()
.AddJsonFormatters()
.AddXmlSerializerFormatters()
.AddCors();
Run Code Online (Sandbox Code Playgroud)
之后,我创建了一个带有 CancellationToken 参数的 API,如下所示:
[HttpGet("list")]
public async Task<ActionResult<IEnumerable<string>>> Get(CancellationToken cancellationToken)
{
var result = await _orderBusinessService.GetList(cancellationToken);
return Ok(result);
}
Run Code Online (Sandbox Code Playgroud)
当我从 Postman 或浏览器调用此 API 时,我得到以下响应:
415 不支持的媒体类型
当我添加[FromQuery]
到cancellationToken时,就可以了。
实际上,这似乎CancellationTokenModelBinder
行不通。
我不知道为什么?有人有任何想法吗?
我正在开发一个ASP.NET MVC应用程序.我的表单模型如下所示:
public class ViewModel
{
public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的表单中,我有以下HTML.
<label class="checkbox pull-left">
<input type="checkbox" id="IsActive" name="IsActive" data-val="true" data-val-required="The IsActive field is required.">
<i></i>Active/Deactive
<input name="IsActive" type="hidden">
</label>
Run Code Online (Sandbox Code Playgroud)
当我发布表单时,模型中的IsActive值始终为false.但是,模型中的Name属性具有值.我通过在以下设置断点来测试这个:
[HttpPost]
public ActionResult MyAction(ViewModel model)
{
// model.IsActive always is false
}
Run Code Online (Sandbox Code Playgroud)
为什么Checkbox值没有设置?
我该怎么做才能解决这个问题?
我正在使用 apache-zookeeper-3.5.5 并且我想运行 zkServer 但我收到以下错误:
当我运行 zkServer 时无法绑定到 8080 端口。
Caused by: java.io.IOException: Failed to bind to /0.0.0.0:8080
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:346)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:308)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:396)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.apache.zookeeper.server.admin.JettyAdminServer.start(JettyAdminServer.java:103)
... 5 more
Caused by: java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:342)
... 12 more
Unable to start AdminServer, exiting abnormally
Run Code Online (Sandbox Code Playgroud)
我的 zoo.cfg 文件在这里:
tickTime=2000
# The number of ticks that the initial
# …
Run Code Online (Sandbox Code Playgroud) 我使用 Asp.Net Core 作为 Rest API 服务。我需要有 API 版本控制。实际上,我在启动以下设置中进行了设置并且它可以正常工作,但是当我设置为默认版本时它不起作用。
services.AddVersionedApiExplorer(
options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
});
services.AddApiVersioning(
options =>
{
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
})
.AddMvc(
options =>
{
options.RespectBrowserAcceptHeader = true;
})
.AddXmlSerializerFormatters();
Run Code Online (Sandbox Code Playgroud)
并在控制器中设置属性,如下所示:版本 1:
[ApiController]
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("1.0")]
public class UsersController : ControllerBase
{
[HttpGet("log")]
public string Get()
{
return $"{DateTime.Now}";
}
}
Run Code Online (Sandbox Code Playgroud)
版本 2:
[ApiController]
[Route("v{version:apiVersion}/[controller]")]
[ApiVersion("2.0")]
public class …
Run Code Online (Sandbox Code Playgroud) asp.net ×3
asp.net-core ×3
asp.net-mvc ×3
c# ×3
.net ×1
.net-core ×1
api ×1
structuremap ×1
versioning ×1