简体   繁体   English

ASP.NET Core Routing适用于VS IIS Express,但不适用于IIS 10

[英]ASP.NET Core Routing works in VS IIS Express but not in IIS 10

I am developing ASP.NET Core web API. 我正在开发ASP.NET Core Web API。 here I have a situation like I have to use multiple get functions which get data from SQL server DB. 在这里我有一种情况,我必须使用多个get函数从SQL Server DB获取数据。 So for that, I'm doing custom attribute routes. 所以,为此,我正在做自定义属性路由。 below are my codes 以下是我的代码

[Route("api/[controller]")]
public class MeController : Controller
{
    private readonly ITechRepository _tech;
    private readonly IPageOptions _page;
    public MeController(ITechRepository tech,IPageOptions page)
    {
        _tech = tech;
        _page = page;
    }

   [Route("getTech")]
    public IEnumerable<TechStack> Get()
    {
        return _tech.getAll();
    }
    [Route("getOptions")]
    public IEnumerable<PageControl> getOptions()
    {
        return _page.getOptions();
    }
     //GET api/values/5
    [HttpGet("{id}")]
    public int Get(int id)
    {
        return id;
    }
}

The above routes are works well in VS IIS Express and this is that URL 以上路由在VS IIS Express中运行良好,这就是URL

http://localhost:51889/api/me/gettech

But when i publish this API in IIS 10. The getTech and getOptions were not working it producing 404 error and also [HttpGet("{id}")] is working in both. 但是,当我在IIS 10中发布此API时, getTechgetOptions无法正常生成404错误,并且[HttpGet("{id}")]在两者中都有效。

Can anybody help on this... 任何人都可以帮忙...

Finally I got the issue fixed. 最后我解决了这个问题。

The problem was in the login of SQL server. 问题出在SQL服务器的登录中。 I configured my application in IIS 10 in the my own Application Pool named aspNetCore . 我在我自己的名为aspNetCore的应用程序池中的IIS 10中配置了我的应用程序。 Its configurations are 它的配置是

.Net CLR version : No managed code .Net CLR版本: 没有托管代码
Managed pipelined mode : Integerated 托管流水线模式: Integerated
Identity : ApplicationPoolIdentity 身份: ApplicationPoolIdentity

Identity cause the issue. 身份导致问题。

When i call this URL http://localhost:51889/api/me/gettech the IIS try to login into the SQL Server with the Login Id of IIS Apppool / aspNetCore . 当我调用此URL http:// localhost:51889 / api / me / gettech时 ,IIS尝试使用IIS Apppool / aspNetCore的登录ID登录SQL Server。 Then ended up with the following error 然后最终出现以下错误

Login failed for user 'IIS APPPOOL\\AspNetCore'. 用户'IIS APPPOOL \\ AspNetCore'登录失败。 Reason: Could not find a login matching the name provided. 原因:找不到与提供的名称相匹配的登录信息。 [CLIENT: ] [客户:]

I captured the above issue in Event Log with Event ID : 18456 我在事件日志中捕获了上述问题,事件ID:18456

For this I have to create the Local User and group for this identity. 为此,我必须为此身份创建本地用户和组。 Unfortunately i can't able do this because i'm using Windows 10 Home edition. 不幸的是我无法做到这一点,因为我使用的是Windows 10家庭版。 which does not allow me to do this. 这不允许我这样做。

what i did is create the Custom Identity for my Application pool(aspNetCore). 我所做的是为我的应用程序池(aspNetCore)创建自定义标识。

在此输入图像描述

在此输入图像描述

There I gave Username, password of the account which is already found in my user groups as well as SQL Server. 在那里,我提供了用户名,已在我的用户组以及SQL Server中找到的帐户的密码。 And i gave dbowner rights to this account for my db. 我为我的数据库提供了dbowner权限。

在此输入图像描述

But still i don't know why it returned 404 error. 但我仍然不知道为什么它返回404错误。

Anyhow, The issue was fixed now. 无论如何,这个问题现在已经解决了。

Happy Coding :) 快乐编码:)

For web api use the Http{Verb} attribute with route template for controller actions as mentioned in documentation 对于web api,请使用带有路径模板的Http{Verb}属性,用于控制器操作,如文档中所述

Tip: 小费:
When building a REST API, it's rare that you will want to use [Route(...)] on an action method. 构建REST API时,很少需要在action方法上使用[Route(...)] It's better to use the more specific [Http Verb Attributes] to be precise about what your API supports. 最好使用更具体的[Http Verb属性]来准确了解您的API支持的内容。 Clients of REST APIs are expected to know what paths and HTTP verbs map to specific logical operations. REST API的客户端应该知道哪些路径和HTTP谓词映射到特定的逻辑操作。

For example... 例如...

[Route("api/[controller]")]
public class MeController : Controller {
    private readonly ITechRepository _tech;
    private readonly IPageOptions _page;
    public MeController(ITechRepository tech,IPageOptions page) {
        _tech = tech;
        _page = page;
    }

    //GET api/me/tech
    [HttpGet("tech")]
    public IEnumerable<TechStack> Get() {
        return _tech.getAll();
    }

    //GET api/me/options
    [HttpGet("options")]
    public IEnumerable<PageControl> getOptions() {
        return _page.getOptions();
    }

    //GET api/me/5
    [HttpGet("{id:int}")]
    public int Get(int id) {
        return id;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net Core Web API 适用于 IIS Express,但在部署到 IIS 时不起作用 - ASP.Net Core Web API works on IIS Express, but not working when deployed to IIS ASP.Net Core 2-Powershell远程处理仅在IIS Express中有效(Kestrel无法正常工作) - ASP.Net core 2 - powershell remoting works only in IIS Express (Kestrel not working) 设置DBContext时出现ASP.NET Core 2.2错误(在Kestrel中起作用,不在IIS Express中起作用) - ASP.NET Core 2.2 error when setting up DBContext (works in Kestrel, not in IIS Express) ASP.NET Core + IIS Express 如何设置 SSL 证书 - ASP.NET Core + IIS Express how to setup SSL Certificate ASP.NET Core IIS Express AD服务权限错误 - ASP.NET Core IIS Express AD Services Permissions Error 如何使用 IIS Express 在 ASP.NET Core 中获取控制台输出 - How to get a Console output in ASP.NET Core with IIS Express Asp.net 核心 - 无法连接网络服务器“IIS Express” - Asp.net core - Unable to connect webserver 'IIS Express' 无法连接到 web 服务器 IIS Express - ASP.NET CORE - Unable to connect to web server IIS Express - ASP.NET CORE ASP.NET 核心 404 错误 IIS 10 - ASP.NET Core 404 Error on IIS 10 运行 exe 的 ASP.NET Web 应用程序在 VS 中工作,而不是在 IIS 中 - ASP.NET Web application running an exe works in VS, not in IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM