gpt4 book ai didi

c# - Asp.Net Core 2.1 获取请求不通过

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:57 25 4
gpt4 key购买 nike

以下代码出现问题,当我在本地计算机上运行它时可以正常工作,但在服务器上发布时无法返回 404。起初我认为这是由于它无法获取文件(路径问题或smth)但它似乎是当我调用这个方法时

http://.../api/Handler/GetUserPhoto?AgentID=XXX

它甚至没有在方法开始时遇到断点。所以我假设 Get 请求没有正确路由。但为什么这只发生在服务器上,我应该在哪里寻找问题的根源?

[HttpGet("[action]")]
public IActionResult GetUserPhoto(int AgentID)
{
try
{
UserPhotoResponseContract response = _Client.GetUserPhotoAsync(AgentID).GetAwaiter().GetResult().Content;

//if picture not found in db then return predefined default picture
if (response.Image.Length < 10) {
string filePath = Path.Combine(_hostingEnvironment.ContentRootPath, "/wwwroot/images", "blank_agent_200.png");

Microsoft.Extensions.FileProviders.IFileProvider provider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(_hostingEnvironment.ContentRootPath + "/wwwroot/images/");
Microsoft.Extensions.FileProviders.IFileInfo fileInfo = provider.GetFileInfo("blank_agent_200.png");
var readStream = fileInfo.CreateReadStream();

return base.File(readStream, "image/png", filePath);
}

return base.File(response.Image, System.Drawing.Imaging.ImageFormat.Png), "image/png");
}
catch (Exception)
{
return new EmptyResult();
}
}

编辑 1

Controller 属性:

[Route("api/[controller]")]
public class HandlerController : Controller

解决方案通过 webdeploy 发布到 IIS 服务器并在 IIS 上运行 Kestrel。

我在 web.config 中设置了这个:

<aspNetCore processPath="dotnet" arguments=".\v10.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout">
<environmentVariables />
</aspNetCore>

但我没有看到它创建任何日志输出。

将方法更改为:

[HttpGet("[action]")]
public IActionResult GetUserPhoto(int AgentID)
{
return Ok("Success");
}

它在做同样的事情,在本地是 200,在服务器上是 404。

编辑 2

IIS 日志:

2019-08-15 12:28:48 10.54.0.12 GET /api/Handler/GetUserPhoto AgentID=781 80 - 10.53.0.157 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/76.0.3809.100+Safari/537.36 - 404 0 2 15
2019-08-15 12:28:49 10.54.0.12 GET /api/Handler/GetUserPhoto AgentID=781 80 - 10.53.0.157 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/76.0.3809.100+Safari/537.36 - 404 0 2 15
2019-08-15 12:29:39 10.54.0.12 GET /api/Handler/GetUserPhoto AgentID=781 80 - 10.53.0.157 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/76.0.3809.100+Safari/537.36 - 404 0 2 15
2019-08-15 12:29:39 10.54.0.12 GET /api/Handler/GetUserPhoto AgentID=781 80 - 10.53.0.157 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/76.0.3809.100+Safari/537.36 - 404 0 2 0
2019-08-15 12:29:40 10.54.0.12 GET /api/Handler/GetUserPhoto AgentID=781 80 - 10.53.0.157 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/76.0.3809.100+Safari/537.36 - 404 0 2 31

编辑 3

启动.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAntiforgery antiforgery)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}

//Middleware for exception filtering
app.UseMiddleware(typeof(ErrorHandlingMiddleware));

app.Use(next =>
{
return async context =>
{
if (context.Request.Path.StartsWithSegments("/test"))
{
await context.Response.WriteAsync("Hit!");
}
else
{
await next(context);
}
};
});

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "/{controller}/{action=Index}/{id?}");
});

//Anti-forgery configuration
app.Use(next => context =>
{
string path = context.Request.Path.Value;

var tokens = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly = false });
//}

return next(context);
});

//Middleware to work with headers
app.UseMiddleware(typeof(HeadersMiddleware));

app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}

最佳答案

我无法发表评论,所以我将发布一种方法来调试您的问题并缩小问题可能来自的位置 - 服务器或 ASP.NET Core 的路由:

让我们通过为作业添加一个简单的中间件来检查您的服务器是否确实收到了任何客户端请求。

在配置方法中:

app.Use(next =>
{
return async context =>
{
if (context.Request.Path.StartsWithSegments("/test"))
{
await context.Response.WriteAsync("Hit!");
}
else
{
await next(context);
}
};
});

确保在 app.UseMvc 之前添加以上代码, 万一。现在,当您启动添加了以下代码的 Web 服务器时,请尝试转至 <yourserver's ip>:<port>/test使用您最喜欢的网络浏览器。

如果您在页面上看到消息“Hit!”,那么您的服务器工作正常并正在接受请求。如果您没有看到任何东西,则表明发生了其他事情,问题的根源几乎总是服务器的配置,除非您为其提供了不正确的 ASP.NET Core 配置。

值得尝试使用 Route 属性而不是 HttpGet 的内置模板来显式声明路由如果一切都失败了。

[Route("[action]")]
[HttpGet]
public IActionResult UserPhoto() // removed input here and also changed
{ // the name slightly just to make it easier to test
return Ok("Success");
}

关于c# - Asp.Net Core 2.1 获取请求不通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57499525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com