Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 941
Sankar Avatar asked Feb 05 '23 19:02

Sankar


2 Answers

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).

enter image description here

enter image description here

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.

enter image description here

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

Anyhow, The issue was fixed now.

Happy Coding :)

like image 68
Sankar Avatar answered Feb 12 '23 18:02

Sankar


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;
    }
}
like image 37
Nkosi Avatar answered Feb 12 '23 18:02

Nkosi