question

ShabbirDaruwala-7855 avatar image
0 Votes"
ShabbirDaruwala-7855 asked ShabbirDaruwala-7855 answered

Scaffold EF Core with Web API

Hi Everyone,

I have done Scaffold using EF Core using WEB Api controller now how can use this to build the cshtml in MVC Application.

Basically when we do Scaffold using EF Core with MVC Controller it automatically creates actionresult with create, detail ,delete and list along with chtml prepopulated HTML Code with model but we how can create or automatically build cshtml if we have Web API

Apologies let me know if it's not clear explained.

Thanks,
Shabbir

dotnet-aspnet-core-mvcdotnet-aspnet-core-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The standard approach is using a documentation tool like OpenAPI (Swagger) with Web API. Take advantage of code generation tool to create a client service.

As for scaffolding, it is uncommon for the MVC UI to mirror Web API result sets. Usually there are models in the MVC application that are used to populate Views and post data from HTML forms. The MVC models are populated by the client service that call Web API. There are mapping tools for this as well like Auto Mapper.



0 Votes 0 ·
ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered ZhiLv-MSFT edited

Hi @ShabbirDaruwala-7855,

If the Web API controller and the MVC controller are in the same project, you could also use the Scaffold to generate the MVC controller and the related cshtml page.

But, from your description, I assume the Web API controller and MVC application are in a different project. If that is the case, in the MVC application, you have to create the view model based on the API return data or required parameter, then based on the view model to create view page.

Besides, you can put the class and the dbcontext in a separate Class Library, then add it in the API and MVC application, then based on the model to create action and view page. Like this:

Here I create three projects: the class library contains all the class and the dbcontext

138881-image.png

Then, in the MVC and API application, I add the class library reference, and install the following package via Nuget:

138859-image.png

Then, add the connection string in the appsettings.json file and in the ConfigureService method, register the DbContext:

     public void ConfigureServices(IServiceCollection services)
     {
         services.AddDbContext<EFCoreLibraryContext>(options =>
              options.UseSqlServer(
                  Configuration.GetConnectionString("DefaultConnection")));
         services.AddControllersWithViews();
     }

After that, I could use Scaffold adding the Controller and views:

138844-image.png

The result as below:

138763-image.png


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Dillion



image.png (148.2 KiB)
image.png (39.0 KiB)
image.png (18.7 KiB)
image.png (159.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ShabbirDaruwala-7855 avatar image
0 Votes"
ShabbirDaruwala-7855 answered

Thanks ZhiLv-MSFT, I am doing exactly what you mention that's only way to scaffold.

Thanks again

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.