Made for educational purposes Asp.Net MVC restaurant app.

Functional: receiving food, writing off products, selling dishes (writing off products based on recipes of sold dishes), transferring products between restaurants.

Now I want to try to make on the existing model a WPF application and later a SPA on Angular.

I decided to make a Web API so that all three clients access the database through it. Experience writing Api before this was not.

If I redo the MVC application, do I have to write the API address somewhere in the settings? In controllers, I will call API methods, get json, generate based on its viewModel and send it to view?

Another idea is to make the viewModel classes for MVC and WPF common. Also make some kind of library for wrapping APi methods or is it better to make one class to call all methods to add dependency injection to MVC?

In the right direction I think?

  • "Another idea is to make the viewCode classes for MVC and WPF common." - for WPF in the view model, you will need to use commands that implement the ICommand interface, to display changing properties in the UI, you will need the implementation of INotifyPropertyChanged . So such an idea of ​​creating common view models for WPF and MVC is hardly reasonable. - Bulson

1 answer 1

If I redo the MVC application, do I have to write the API address somewhere in the settings? In controllers, I will call API methods, get json, generate based on its viewModel and send it to view?

To be able to add an api-контроллер to an existing MVC project, follow the steps described here , namely:

  1. Through NuGet install Microsoft.AspNet.WebApi ;
  2. Add the class App_Start\WebApiConfig.cs with the definition of api-маршрутов , with the following contents:

     public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); } } 
  3. In the Global.asax.cs file, you must call the registration of api-маршрутов first of mvc-маршрутов . To do this, we call GlobalConfiguration.Configure(WebApiConfig.Register) before RouteConfig.RegisterRoutes(RouteTable.Routes) ;


Now, speaking within the SPA application, after we configure the routing in the AngularJS module (for example):

 var exampleApp = angular.module('exampleApp', ['ngRoute']); exampleApp.config(function ($routeProvider, $locationProvider) { $routeProvider .when('/example', { templateUrl: 'Templates/example.html', controller: 'exampleCtrl' }) $locationProvider.hashPrefix(''); }); 

we can (for example) go to example.html by calling <a href="#/example">Example</a>