1
votes

I am busy creating an ASP .Net MVC 1.1 Web App. This will server as the front end. I am also creating an ASP .Net MVC 1.1 Web API. This will server as the back end to the web app, as well as to a phone app that a colleague is developing.

My question is, the web app and web API share many of the same models. For example, in a car dealership, the web app might have a form to add a new car. The user would fill in all the fields in the form and hit submit. The web app would then perform an HTTP POST to the Web API, passing this car model to the Web API controller, which would receive the car model as a parameter. So, in this case, both web app and web API would have a copy of the car model. What is the correct way to handle this? Do I need to have a car model in the "Models" folder of both my web app project and my web API project in the "Car Dealership" solution? And if I have to add a field (such as mileage) I would add it to both models (as well as to the database and to the HTML form)? Is there no way to share models?

Or maybe I hope my logic all wrong? I don't know... I just want to make sure I'm doing things correctly - I'm very new to ASP .Net (and web development in general).

Thank you

1

1 Answers

1
votes

I would suggest:

  1. Asp.net Web API (Rest API)

    • Create your Models with EF and Ado.net (it will autocreate a model for each database table)

    • Add API-Controllers for your models

    • Configure your API and publish it on e.g. IIS
  2. Asp.net Web App & phone app

    • Create your wep-app & phone app and send HTTP-Requests to the API

The RestAPI knows the Models, the WebApplication and Phone APP only need to know what to send in a Request

enter image description here

I hope this helps.