I have created a project using ASP.NET Core Web Application (.NET Framework). My project structure:
My values controller is inherited from an MVC controller. When I hit http://localhost:20798/api/values, I do get the desired response.
I added ConsumerScore controller, this time a WebAPI controller inheriting from ApiController. But now when I hit http://localhost:20798/api/consumerscore, I am getting 404 response.
My ConsumerScore controller looks like:
How to resolve this error?
The error is with the "ApiController" you should use just Controller in your implementation.
There is indeed to particular ApiController class anymore since MVC and WebAPI have been merged in ASP.NET Core. However, the Controller class of MVC brings in a bunch of features you probably won't need when developing just a Web API, such as a views and model binding.
for more see: Is ApiController deprecated in .NET CORE?
Seems like you are confusing the idea of "MVC" and "Web API".
ASP.net MVC is used for serving web pages while the Web API is supposed to be serving data in a negotiable way (json/xml/...) to http requests.
Adding an "api/*" to an MVC route and serving data from an MVC Controller is not the best idea and will not turn it into a useful web service.
From the look of your methods, I suppose what you need is an ApiController but you cannot create the http request by copying the "~/api/*" to your web browser and you need to use tools like fiddler or your browser's debugger.
Related
I am creating an asp.net web api project, I am storing my data in mssql server db, and I am using an Entity Framework. I was following the microsoft guide (https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio), it uses an api controller with entity framework scaffold item. In other guides I found people using controllers derived from ApiController class and the routing there is made via web.config file. What is the actual difference, and what should I use? I suppose I should follow the MC guide cause it is fairly new (was added a week ago).
P.S.: right now, I am not planning to add "view" part to my app, first I want to create all the API calls, but later I whould like to.
Asp.net6 brought new ways of working with controllers, detaching a little from the MVC pattern we had, but this model of yours, if you are following the tutorial, ends up inheriting from the MVC standard ApiController, look at this part of the official tutorial:
Marks the class with the [ApiController] attribute. This attribute
indicates that the controller responds to web API requests. For
information about specific behaviors that the attribute enables, see
Create web APIs with ASP.NET Core. Uses DI to inject the database
context (TodoContext) into the controller. The database context is
used in each of the CRUD methods in the controller. The ASP.NET Core
templates for:
Controllers with views include [action] in the route template. API
controllers don't include [action] in the route template.
well this implies that your controller is being mapped into the aspnet mvc stream and being registered there when you call app.MapControllers(); in your Program.cs
and it's not necessary to configure anything in webConfig, which doesn't even exist anymore since the framework
You can follow the tutorial and everything will be fine.
I am new to asp.net mvc world. So I have a question
I have already developed a web app using Asp.net MVC (also deployed on production).
Currently I am working on mobile apps. For this I need web services (restful).
For Restful web services do I have to make a new project (within existing solution) or can I incorporate Restful webservices into my existing (Asp.net MVC) project ? (I prefer 2nd option if possible)
If I have to make new project for Web Api, then how will I deploy both projects on production knowing that Web Api project is dependent on Asp.net-MVC project ?
One thing you need to understand first is whether it's a web service,wcf service or a Web API the only thing you need is to get a json/xml output which you can use in your mobile app.
Let say you have and asp.net mvc application which has some action methods, but you might be returning a View or PartialView which is not you want for a mobile app to parse. So you need to create an action method which returns JsonResult.
If you want to use all the RESTFul verbs like POST,PUT,GET,DELETE you can add another controller which inherits from APIController and write methods there, but either ways output is same.
So it's up to you what to do and how to proceed, only thing is with an APIController you will have some more verbs and code ahve some special returns like "Ok" e.t.c
I have a client dashboard application, written in ASP.Net MVC 4 (not bound to this version, happy to upgrade). The website is hosted in Windows Azure.
Our clients would like to programmatically access this dashboard, via an API.
Affectively they would like to be able to perform all of the same functions which they normally carry out on the dashboard website, programmatically from code using an HTTP Restful Service.
....My instant reaction was to simply build an ASP.net Web API project, and separate out the shared services/libraries/components from the existing MVC project so that both the API and the MVC website can call the same code base.
Questions
Is it possible to simply create Web API controllers within my existing MVC website project, and expose them over HTTP?
If it is not possible to do "1.", will Azure play nicely if I have an MVC solution, a separate Web API solution, and a shared library project of common services and models? How will I ensure that Azure copies the shared library components into the cloud when I deploy the MVC solution and the Web API solution separately?
Update
Based on the comments, the following implementation of two separate controllers (the original MVC controller within the MVC project, and an additional Web API controller within the same project), does in fact work. Please note that based on the following LINKED ARTICLE, the below implementation would be a "pre-MVC 6" implementation. MVC 6 itself provides the ability to implement both API calls and normally MVC View calls, into a single controller (as opposed to separate controllers that inherit from different base classes).
public class ProductsController : Controller
{
//Products/Index
public ActionResult Index()
{
return View();
}
}
public class ProductsAPIController : ApiController
{
// GET api/productsapi/getall
public IEnumerable<string> GetAll()
{
return new string[] { "value1", "value2" };
}
}
To be fair - MVC or WebAPI, as far as your consuming clients are concerned, it shouldn't matter.
Both MVC and WebAPI can create JSON or XML outputs - it's just one makes it a bit easier (WebAPI serializes the relevant response based on the client).
But you can quite easily mix the two together, as others have said, it's as simple as Add New Item
The newest version of MVC (6) has now integrated both technologies into one solution. http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6
This would be ideally what you are looking for if you can start a new project.
Question 1: Yes it is, just take a look at the article above.
Question 2:
Azure will be fine if you have a MVC solution and also a WebApi solution, but I think you will need to run them under different websites (I'm not 100% on this).
Yes it will work fine. The only potential gotcha is you have to clearly separate your WebAPI routes from your MVC routes (like, for instance having all WebAPI routes under a /api/... prefix, this is the default).
If you do decide to do two separate projects for whatever reason and want to have a shared library, the best solution is to keep that library in a NuGet package on a private feed. TeamCity supports this out of the box, along with other nice features (continuous integration/deployment).
What is the big difference between normal asp.net mvc and asp.net web api ? When i know i can use a normal action result to return a Json data and call it with javascript or an ajax onsuccess function or even with jquery GetJson() function.
Asp.Net MVC is used to create web applications that returns both views and data but Asp.Net Web API is used to create full blown HTTP services with easy and simple way that returns only data not view.
ASP.NET Web API vs. ASP.NET MVC “APIs”
Here is similar post ASP.NET WebApi vs MVC.
The answer is not accepted one, but the other one.
Or check this or this.
I work on single page app project with API, which is located under mydomain.com/api/ url. Now I need to have separate API for other purpose e.g. mydomain.com/exchangebox/api/.
Is it possible to have both APIs controllers in same WebApi project?
If so, how the application knows which controller to use, if I request /api/person/ or /exchangebox/api/person?
In answer to your questions.. specifically:
1) Is it possible to have both APIs controllers in same WebApi project?
Yes. It is.
2) If so, how the application knows which controller to use, if I request /api/person/ or /exchangebox/api/person?
Routes. MVC and WebAPI is based on routing. I would suggest you read a tutorial or two on what Routes are and how they work.
For example, this tutorial on the ASP.NET website: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api