C# MVC and API in one project - c#

I'm going to create a C# Web Application using MVC. I also want to have an API which mirrors the Web UI. As an example: if I have a web ui to "Create employee" I want a matching API call which does the same. It would take in the same information and follow the same process. I want to avoid duplication of code, so I'm looking for guidance on how to best structure my VS Project, so that I start the project correctly. Where should I put my models, my controllers etc? All suggestions appreciated.

I would use ViewModels for the Views and the Api. This viewmodel is used to return to the browser as JSON or to build the View.
I would then use a library for the code handling and redirect to the libray in the Action methods in the view controller.
The View controller will recieve the ViewModel from the API or normal Action methods. It could pass this viewmodel along to the library or possibly translate it to a datamodel and then pass it to the library. In the same action method the view controller will get a model back from the library and rebuilds the viewmodel.
If you use a ViewModel the Api can easily translate it to and from JSON.

Related

How to separate the view and the controller in an ASP.NET application

I have an ASP.NET application that was developed by "programmers". This application contains all things which you should not do:
Hardcoded settings
Copy/paste anywhere (code not re-used)
Make a lot of small SELECT requests to the DB for each row instead of doing JOIN
Model, view and controller in one function
Etc.
Now, my goal is not to throw everything away and start over, but I want to separate different aspects of the MVC of the application. I do not want to start a new MVC project, I would like to evolve the existing solution into something modular.
For the controller, there is no problem, I can create classes that will manage DB connections, send mails etc. On the other hand I do not know how to separate the view and the controller.
The problem that traditional ASP pages myPage.aspx have an associated file myPage.aspx.vb and in this vb file there are both view management part(page elements, like dropdowns) and also the Business part (controller) which is executed on the button click.
I thought about making a call to a myPageControl.vb class that will contain the business part from the file myPage.aspx.vb, which will call the Model (db, mail, other).
(View: myPage.aspx.vb) -> (Control: myPageControl.vb) -> (Model: Db.vb, Mail.vb)
The problem is: how should I do to modify the page content from the controller, for example change a list value or display a text on it. I have to make a call to the View (to the other direction) and pass by parameter the class MyPage (.asp.vb)
I have already tried to find an answer to my question, but I've found only answers taking about MVC projects.
Does anyone have any idea how I should do it?
Seperation of Concerns was one of the main problems with webforms, and one of the advantages of MVC. In my opinion the best you could probably do is seperate the business logic into classes like you are doing now so code could be reused throughout the application, but completely "seperating" everything may require rebuilding the application as an MVC app.
The only answer I've found to this is to have the controller send the "data to bind to" to the page as XML. then all the page has is its page_load of course, and then a method to receive the XML and update itself from it. You can use smart naming structures so that you can do reflection and autobind from the xml to page elements.
Then on an action, have the page generate an xml of all the elements on it and their values, and send that through a "ProcessAction" method that determines the correct controller and calls the right method on the controller.
But as suggested, doing it over as an MVC project probably makes the most sense if that's the pattern you are going for. What I suggested works, but it will be as much or more work than just starting from scratch with MVC. Besides, remember that "support" for web forms is disappearing soon. It's not in (and won't be in) .NetCore, so it's days are numbered.

Umbraco and MVC and JSON

So I got pulled into a deep end having zero experience in umbraco and a tight deadline.
I don't know how umbraco works and how you integrate your MVC site into it really. A lot of headfuzz to get around.
basically the person I am inheriting it from created a basic controller from MVC and we can call the MVC site as we normally do.
I can also do JSON calls to the controller action which gives us back some data in a ViewModel. Great.
But when you browse to the site using umbraco and navigate to that same page, we run into big problems such as not being able to invoke the JSON call to get the data as it says object not found (in other words, the controller action is not found).
I read about umbraco basically overriding the default MVC routings but... why such a mess? :)
how can I integrate an existing MVC site into umbraco without much pain?
what is the url to call a controller action in umbraco integration?
say we have this:
public JsonResult GetPersonDetail(int id)
{
var vm = new AjaxPersonDetailViewModel(....);
return new JsonResult( Data = vm };
}
I can call this in JQuery like so:
/MyController/GetPersonDetail/1
so how do I do that with Umbraco?
Also try using this scaffolding package as well to get the initial bits for your controller in the right place http://our.umbraco.org/Documentation/Reference/Mvc/scaffolding.
Yes you can just manually create the files that the scaffolding process creates and reference them accordingly however i prefer to use that first then add/tweak the auto files to match what i need

How to connect Controller files to Views using .NET 4.0 MVC3

I have spent a few hours searching around however I must be either searching for the wrong thing or doing something wrong because I can't seem to find what I am looking for.
This seems like a newbie question (and it probably is). I have created a C# file called dbconnect.cs and I am now trying to figure out how to connect everything together. My file structure is as follows...
-Controllers
-AcountController.cs
-dbconnect.cs (not sure if this should go here or in models folder)
-HomeController.cs
-Models
-AccountModels.cs
-Views
-Account
-Home
-About.cshtml
-Index.cshtml
-Reviews.cshtml
So basically what I am trying to figure out is a couple things.
The proper way to use databases with MVC3 (should my file be in Controllers or Models)
How to connect my dbconnect.cs folder with my View so I can troubleshoot it.
Any help / useful documentation sites is much appreciated. Thanks in advance.
EDIT:
I am not sure if this is relevant or not. I am using MySql and not SQL. Also I am assuming i need to use using dbconnect; but I am not sure how to integrate it with the HTML files.
For databases, you should use Entity Framework.
For views, each controller should have a views folder with the same name (eg, ~/Views/Home/ for HomeController), with one view file for each action.
You can also exlicitly pass a view name to return View().
You should really try to understand the functionality of Models, Views, and Controllers.
Start with understanding Controllers, then Views, and then Models and then everything else will be clear:
=================Controllers=================
Controllers are the middle-man between your User Interface (UI) and the "Back-end".
Inside of a controller, you define what happens when a user requests something from a certain URL.
Controllers usually are tied to the URL.
Meaning your HomeController function TestFunction will RUN (GET) when you go to the URL:
/Home/TestFunction
=================Views=================
Views is the User Interface. The HTML. The "What it will look like"
A view usually gets its "data" from the controller.
What is this "data" - well that's the model:
==============Models=================
The data between being transferred to the View (UI) from the Controller.
Your code should get data in the controller and should return as a model to the view
By the way, I would use EF (Entity Framework) to manipulate the data and make calls to the EF from the controller (usually via a HelperClass)
Good Luck!
Assuming dbconnect.cs contains class definitions within a namespace like this:
namespace MyProject.DbConnect
{
public class Repository
{
// ...
}
}
In your controller class files you can simply add using MyProject.DbConnect to reference anything from that namespace. And if you want to do that from the views, simply add #using MyProject.DbConnect
That being said. You really should be using a mature ORM like Entity Framework.

WCF as MVC controller. model and views?

I've been searching on the net for possible answer to my question, but no luck at all.
I just wonder if I could use WCF as my Controller in ASP.NET MVC.
So if I call
(WCF)
http://localhost:1621/WCF/LogOut.svc?id=10001
It will work like
(MVC)
http://localhost:1621/WCF/LogOut?id=10001
Or I'm just having wrong thought about it?
Can you give some source for deeper knowledge in MVC and WCF.. Thanks!
The MVC states that the controller picks up requests and it is responsible for preparing the model and passing it to a view which is ultimately rendered.
Although from the perspective of web requests, WCF could possibly look like MVC, there is no easy way to make WCF render the HTML to the browser. This means that if WCF can implement "controllers" which produce "models" but is not designed to create "views".
However, if your web application can pick up XML/JSON data produced by WCF calls (i.e. you have your views implemented purely at the client side) then yes, WCF does a good job as a provider of the "controller/model" part of the MVC.

ASP.NET MVC ViewModel and Request Variables

Greetings!
I need assistance with ASP.NET MVC and the values posted from a form operation (form is inside a Partial View).
On my page to create a User Account, various form fields collect the information. The Partial View is backed by a ViewModel (UserAccountViewModel).
To validate and/or save the information, I need to pass the values of the form to UserAccountService. Back in Java and Struts 1.x, I used the getMap() method of the DynaActionForm, but being an ASP.NET newbie, I'm not sure of the best way to proceed.
On a post operation, are the fields of the ViewModel automatically updated? If that's the case, I could pass the ViewModel to the Service layer (not my preferred solution, but good enough).
Jason
You can use the UpdateForm method in .net to have it automatically map the form data to your model based on similar naming.
UserAccountViewModel.UpdateForm(Request.Form);
UpdateForm will work on pretty much any class with properties, you just need to import the namespace. Here is a pretty good tutorial on form handling in asp.net mvc that uses this method. This sounds like it's a similar method to what you're used to in Java.

Categories