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

╄→гoц情女王★ 提交于 2019-12-06 10:20:03

问题


I am developing 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. 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

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.

Can anybody help on this...


回答1:


Finally I got the issue fixed.

The problem was in the login of SQL server. I configured my application in IIS 10 in the my own Application Pool named aspNetCore. Its configurations are

.Net CLR version : No managed code
Managed pipelined mode : Integerated
Identity : 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. Then ended up with the following error

Login failed for user '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

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. which does not allow me to do this.

what i did is create the Custom Identity for my Application pool(aspNetCore).

There I gave Username, password of the account which is already found in my user groups as well as SQL Server. And i gave dbowner rights to this account for my db.

But still i don't know why it returned 404 error.

Anyhow, The issue was fixed now.

Happy Coding :)




回答2:


For web api use the Http{Verb} attribute with route template for controller actions as mentioned in documentation

Tip:
When building a REST API, it's rare that you will want to use [Route(...)] on an action method. It's better to use the more specific [HttpVerbAttributes] to be precise about what your API supports. Clients of REST APIs are expected to know what paths and HTTP verbs map to specific logical operations.

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;
    }
}


来源:https://stackoverflow.com/questions/43017478/asp-net-core-routing-works-in-vs-iis-express-but-not-in-iis-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!