atifaltaf atifaltaf - 3 months ago 9
ASP.NET (C#) Question

WebApi is not Working in ASP.Net

I am New in Asp.Net and tried to develop a small Web API in learning process.


WebApiConfig.cs


config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);



TopicsController.cs


namespace MessageBoard.Controllers
{
public class TopicsController : ApiController
{
private IMessageBoardRepository _repo;

public TopicsController(IMessageBoardRepository repo)
{
_repo = repo;
}
public IEnumerable<Topic> Get()
{

var topics = _repo.GetTopics()
.OrderByDescending(t => t.Created)
.Take(25)
.ToList();
return topics;
}
}
}


Actually i am watching PluralSight tutorials.

http://localhost:50031/api/v1/topics


this Url is not working in Browser not in Fiddler 4.

all references are added. i have also done Build Solution but its not working and their is no error showing in the code.

Answer

One last step to enable Web Api which looks like you're missing is enabling Web API in the Global.asax file by adding the following line of code to the Application_Start() method:

WebApiConfig.Register(GlobalConfiguration.Configuration);

Also, please don't use the port number from the PluralSight tutorial.You need to run the web application project from your instance of Visual Studio and when it opens up in the browser you will see which port is assigned to YOUR api service.So if you see that it assigned port 12345 for example you would call the following URL to access the service action:

http://localhost:12345/api/v1/topics
Comments