Equivalent of WCF UriTemplate in ASP.NET MVC4 WebApi - asp.net-mvc-4

Am migrating a WebApi project (from preview version aka WCF WebApi to final release aka ASP.NET MVC4 WebApi).
In WCF WebApi, URI templates were defined for all services operations, like:
[WebGet(UriTemplate = "{movieGenreId}/movies")]
Bare I mind I have tens of those in the project. How do I translate this to MVC4 WebApi without having to register tens of entries in the Route table?

You could go with the excellent attribute routing for Web API project:
Here is the source on GitHub (both MVC & Web API) - https://github.com/mccalltd/AttributeRouting
Here is a short intro to attribute routing in Web API - http://www.strathweb.com/2012/05/attribute-based-routing-in-asp-net-web-api/
Here is the Nuget for the attribute routing Web API package - http://nuget.org/packages/AttributeRouting.WebApi/3.1.2
It should be almost as easy as search and replace to convert UriTemplates to Attribute Routing.
If you are not willing to include the attribute routing library in your project, unfortunately the only other way is - as you said - registering tens of routes.

Related

Migration of ASP.NET 4.6 Help Pages to .NET 6 (.net core)

In .NET framework we have support of Creating Help Page for Web API.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages
Does this feature supported in .NET 6. We are migrating our legacy ASP.NET framework application to .NET6.
How to migrate this feature to .NET6? If it is not supported in .Net6(.net core) how can we achieve the similar functionality in .Net core
I am trying to migrate this feature to .net core but I am facing issues on how to load the app data, register HelpdataConfig in .net core.
ITNOA
As you can see in ASP.NET help page for ASP.NET Core Web API, the Microsoft.AspNet.WebApi.HelpPage is for ASP.NET and does not for ASP.NET CORE or .NET 6, so you have to migrate this library to some popular Web API documentation libraries like Swagger
As you can see in ASP.NET Core web API documentation with Swagger / OpenAPI, you can use below documentation
By Christoph Nienaber and Rico Suter
Swagger (OpenAPI) is a language-agnostic specification for describing REST APIs. It allows both computers and humans to understand the capabilities of a REST API without direct access to the source code. Its main goals are to:
Minimize the amount of work needed to connect decoupled services.
Reduce the amount of time needed to accurately document a service.
The two main OpenAPI implementations for .NET are Swashbuckle and NSwag, see:
Getting Started with Swashbuckle
Getting Started with NSwag

Web Application vs Web Api project types in Asp.net Core

I know that Asp.Net MVC and Asp.Net Web API were merged into one code in Asp.net Core and they inherit from Controller base class and can all return implementations of IActionResult. it be a View for MVC or Json for web api.
But when i want to create a Asp.net Core project, it offers two templates (Web Application and Web Api ), according to what i said in above, there is no differences between these controllers, why there is two templates? is there any differences that i don't know about it?
The web application template will create folders and import stuff needed for a web application such as jquery, css etc. Web api template will create folders and import stuff for a web api. Also the controllers created by default will have different implementations, for example, web application will be returning views and the views will be created in the appropriate folder.
So although they derive from the same controllers, each type of project requires different dependencies.
If I were you I would go ahead and create one for each type and see the difference.
If you want to have both web api and web application in the same project, use areas. This way your web and api will have separate controllers, folders and models. Also if you want to separate them in the future, it will be easy to do so.
The difference between 2 templates is-
The WebAPI template starts with a Controller class that will allow you to respond to RESTful requests at the /api/Values endpoint.
The Web Application template will give you an MVC framework enabled project with some Razor views, the bootstrap CSS framework and jQuery library installed.
If you want to create project with both MVC and API controllers then I would suggest to go with ASP.NET Core Web Application template and add require dependencies.

Why people mixed ASP.Net MVC and web-api in same project

i need few reason for which people mixed ASP.Net MVC and web-api in same project. when we can develop a full project in mvc only then why web api need to include. also we can host webapi project separately which can server request to MVC and other devs or mobile devs etc.discuss the reason and advantages.
some one answer :
We have recently built a project within MVC and WebApi, we used the WebApi purely because from a Mobile Apps perspective. We allowed the mobile dev guys to call our methods within our MVC application instead of them having to go and create the same function.
WebApi allows to create services that can be exposed over HTTP rather than through a formal service such as WCF or SOAP. Another difference is in the way how WebApi uses Http protocol and makes it truly First class Http citizen.
still the above answer is not clear to me and i think this is not the reason for which people mixed ASP.Net MVC and web-api in same project.
so anyone mind to discuss the actual reason and advantages with example scenario.
thanks
Each have a purpose. Most of the time it's usually caused by legacy code. I know we included documentation which used MVC, but we're fully webapi.
FYI, was MS's auto documentation for WebApi ironically.

Introducing WebAPI in ASP.NET MVC4 application

I am working on an MVC4 application that uses MVC controllers and it is already in production. But a new requirement is to expose some of the functionality as a web service so it is accessible to get/post data programatically by other applications. So I am thinking of introuding WebAPI in this application and use it to provide an iterface to external world as well as use the same service by the views in my application.
My question is how can I use WebAPI in my existing controllers so that I can return Views instead of raw Json data.
Thanks

webApi vs SPA .NET MVC 4 controller

So I'm about to create my first SPA project using .NET MVC4. But I just need a little clarification before I start. It seems from a few tutorials, SPA is built based on WebAPI architecture.
Is it safe to say SPA = WebAPI + knockout.js + history.js +
upshot.js which all together function as a web application that can
run somewhat offline? so later down the road if we want to build a
native phone app, we can always just call it from the SPA
DataServiceController?
What's the difference between webAPI controller
(that inherits from ApiController) VS SPA controller (that
inherits from DbDataController<..>)
SPA works with WebAPI, history.js, upshot.js. Knockout is not necessarily required (i.e. you can choose your own client-side framework. From what I understand, you need to use Entity Framework as well.
DbDataController is a generic type that requires specifying the DataContext you want to expose. This is not required with ApiController. It seems that there SPA requires using EF, which seems restrictive in my opinion.