'WEB API Controller not loading in browser

Having some issues getting started in C#, here's the error I'm getting: HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

New to this language, any tips appreciated.

Here's my code:

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Http5112Assignment2.Controllers
{
    public class DiceGameController : ApiController
    {

        [Route("api/J2/DiceGame/{die1}/{die2})")]
        public class J2Controller : ApiController
        {
            [HttpGet]
            public int DiceGame(int die1, int die2)
            {
                int dieSum = die1 + die2;
                return dieSum;
            }
        }

    }
}


Solution 1:[1]

I see that there may be a typo in your route string, note the ) in the /{die2})

So, Assuming that you didn't change anything in the default routing in your RouteConfig file, then Make sure you're URL is as follows (note that mine runs on https://localhost:44301 yours may differ)

https://localhost:44301/api/J2/DiceGame/12/34)

Note: It'd be helpful if you provide the request URL you were attempting to use that gave you the error or even better a cURL which is a standard format for that request that gave you the 404

Solution 2:[2]

Kindly do the following.

  1. Move each class to a separate file with the name as the Api controller. In here we can see that you have nested two Api controllers which should not be done.

  2. Api controller route annotations usually looks like [Route("[controller]")] or [Route("api/[controller]")]. Below is an example.

        [Route("api/[controller]")]
        public class J2Controller : ApiController
        {
            [HttpGet("DiceGame/{die1}/{die2}")]
            public int DiceGame([FromRoute] int die1, [FromRoute] int die2)
            {
                int dieSum = die1 + die2;
                return dieSum;
            }
        }
    

If you want to call the DiceGame endpoint, then make sure your Api is running and you can simply do a HTTP GET request to the URL: https://localhost:[yourport]/api/j2/dicegame/69/420. For more info on routing, visit the following URL. Create web APIs with ASP.NET Core

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EvilNeo
Solution 2