Best practice webapi/website in one project - c#

I am creating a new website and web api. Using the VS2013 template I select the webapi template (with individual login). The template creates a mvc asp site.
Is there anything wrong with having my website in the same project as my web api or should I create two projects in my solution. One for the API and one for the website?
I am thinking of allowing a desktop app to use the api as well.

As Brian said there is nothing wrong, but it is not only about code organization, if you host them in same project then there is no need to enable CORS for API if you are building SPA (issuing XHR requests from browser), they both will be on the same domain.
As well from security perceptive it will get complicated because you will use cookies for the MVC application and bearer tokens for the API. I'm not big fan for the VS 2013 Web API template with individual accounts because it add lots of middle wares and boilerplate code that you might need to use or not aware of its functionality.
My recommendation is to separate them from the beginning and have control on both projects.
You can read more on how to build API from scratch and components you need here.

No there is nothing "wrong" with including the WebAPI code in the ASP.NET MVC project. At its simplest, having the WebAPI in a separate project is merely code organization, having any code in another project is about code organization. If your project is small, will remain small, and you are the only one working on the project than it really is fine to leave API in the same project as the ASP.NET MVC website.
However, if you expect other people to work on the project, if you expect the code base to grow. Than you should organize your code well from the start and place the WebAPI in a separate project.
But for a shorter answer: it is wrong, you should always be tightly organizing your code at every moment or it will get out of control, be disorganized and hard to maintain in the future.
So put the WebAPI in a separate class library project.

Related

Starting an ASP.NET Core web project with API and razor pages

I am a beginner in web development and I want to start a new ASP.NET Core project and I have some questions regarding the structure of the project and what is the best practice. So, my goal is to make a website using razor pages and web API all together. I thought about 2 options:
To make different projects for the API and for the razor pages and between them to make a C# library that has an API Helper which sends the HTTP requests to the API project.
To put the API and the razor pages in the same project. I think this option is much simpler but I have some concerns. How to call the API? (I saw that you can use jQuery or in the same way I can make an API Helper class). If I use jQuery for API call can I do the validation in C# before jQuery send the data to the API?
Another thing I want to ask is should I use Identity for authentication? I don`t want default pages and tables in my database when I do the migration. I want to customize them. I know that I can modify the Identity classes and pages if I scaffold them but I want to do it manually. I also know that making such an authentication system could be challenging but I want to learn how to do it.
To sum up, I am curious to know how are ASP.NET CORE web projects structured in development and I want to know some best practices to do all these things. Thank you in advance!
I think you maybe misunderstand how razor pages works. Its a purely server-side rendering system. The client requests a page from the server, and the razor pages renders the whole page and sends the resulting HTML, CSS, JS to the client browser. This means you dont need to think of an API as a separate piece really. Unless you want to make it more complicated, a Razor Pages website will be a single project, where each page has two files, a View (.cshtml) file which combines c# and html to build the web page, and a backing .cs file which handles the routing and assembling of data to provide to the view, along with handling authentication etc.
As for authentication, I recommend you go with Identity if you plan on actually deploying this live, rolling your own authentication system out is difficult and error prone, and Identity is easy to customize to your particular needs, while also giving a good and secure starting point.

Integrate Angular with MVC in existing ASPX project

We have an existing Application in ASP.Net (Aspx Pages), we are thinking to integrate new functionality in MVC. This is something that we can easily do it through the several examples available on the Internet.
Here, the main question is "What if we want to integrate MVC project with Angular?" We could not find any example or blog related to the same.
Is it possible? What are the pros and cons of it? How we can achieve it?
We are totally blank in this, any help would be appreciated.
Note: We do not have any code because we haven't found anything.
You have to consider following things at least:
Routing (Angular has its own routing configuration which can be tweaked if needed).
Angular (not AngularJS) uses TypeScript. So, in your existing ASP.NET project, you have to ensure that your TypeScript files are transpiled into JS.
Since you say that the existing ASP.NET project is working, it might make sense to think about why you would like to do this, what your objectives are.
AFAIK, using Angular in ASP.NET project directly (without using MVC alongside) makes more sense.
I think you can do the following:
Use Angular for client-side rendering, calling HTTP
APIs and UX. Angular allows re-usable components written in TypeScript and makes architecture modular.
Continue with ASP.NET for server-side processing and security.

Creating a Project in VS19 to work with an Angular Frontend and ASP.NET Backend

I am trying to set up an Angular8 project to work with an already existing WCF Service and ASP.NET backend. The goals being to get the project set up in Visual Studio 2019. In addition to getting the actual project set up how to then communicate to the WCF Service from the Angular Project to receive the data?
I have tried various tutorials but most of them use ASP.NET Core instead of just ASP.NET. I have gotten far with setting up a test project with a ASP.NET MVC application then forcing it to route to Angular but that is still posing some problems. If someone has some advice for setting up the Angular project to work with just an empty ASP.NET project that would best. None of the test projects I have created thus far are exactly what I am looking for, so posting them here would just cause confusion.
Expected: A demo project to prove that a project can be set up where Angular8 can communicate to the WCF Service, get some data from it then display it.
You can try to use ASP.NET Boilerplate Framework: https://aspnetboilerplate.com/ this framework starter templates for both types of ASP.NET projects: Core and MVC.
ASP.NET Boilerplate (ABP) is an open source and well-documented application framework. It's not just a framework, it also provides a strong architectural model based on Domain Driven Design, with all the best practices in mind.
I believe that is a good way to start. Also you can create project using either: Angular, Vue or React.
Hope that helps you. Good luck!

Multiple WebApp projects within the same solution

I currently have a VERY large webapp project, a DAL layer and a BLL layer and two testing projects in a solution together. I would like to start moving away from ASP.NET webforms and begin moving over to MVC4 with Razor.
I have tried going down the hybrid project route by adding in the correct routes, folders and project guid's into the csproj, but, this seems like a bit of a hack.
Is there a way i can have a Webforms project and a MVC4 project within the same solution, and have them play nicely together when deployed. I.e. if one cannot handle the request given, the other tries to handle it. This way, i am free to migrate the webforms project to MVC over time.
Is there a way i can have a Webforms project and a MVC4 project within
the same solution, and have them play nicely together when deployed
With ONE ASP.Net and VS 2013, you can make Webforms, MVC and Web API play together pretty nicely. Check out this reference.
if one cannot handle the request given, the other tries to handle it.
There is no IN-THE-BOX solution for dynamic switching between MVC and webforms. You have to use concepts like HttpModules or HttpModules or Routing etc., to get to have customized request handling.

Architecture for new ASP.NET web application

I'm maintaining an application which currently is just a web service (built with WCF) and a database backend. The web service is built in layers with a linq-to-sql data access part with core functionality in an own assembly and on top of that the web service assembly which contains the WCF code. The core assembly also handles all business logic rules (very few actually).
The customer now wants a Web interface for the application instead of just accessing it through other applications which are consuming the web service. I'm quite lost on modern web application design, so I would like some advice on what architecture and frameworks to use for the web application. The web application will be using the same core assembly with business rules and the linq-to-sql data access layer as the web service.
Some concepts I've thought about are:
ASP.NET MVC (or MVC-2)
Webforms
AJAX controls - possibly leting the AJAX controls access the existing web service through JSON.
Are there any more concepts I should look into? Which one is the best for a fresh project?
The development tools are Visual Studio 2008 Team Edition for Developers targeting .NET 3.5. An upgrade to Visual Studio 2010 Premium (or maybe even Ultimate) is possible if it gives any benefits.
Definitely dig into ASP.NET MVC2.
All of our projects are now being developed using ASP.NET MVC2. It's not just highly scalable. It's highly testable as well. Which leads to way better maintainable apps in the long term.
WebForms vs. MVC2 points - (speaking out of experience):
Scalability:
In our company we had a lot of applications using WebForms which then were updated and changed by us as needed by our customers.
I think your customer will be requesting more changes on the application in near future. Making calls to other services, and maybe you'll have to rework parts of the final product to match their wishes.
And with the upcoming Cloud Computing and the Windows Azure platform you'll probably need to keep up with your code.
ASP.NET MVC absolutely supports the concept of being able to scale up your application any time you want.
I remember one of our customers walking up on me asking me for an extension for their app (they have a member management system) and the feature would contain something like a link to export the current view as a csv file so they could do office stuff with it (mostly serial letters).
It wasn't really a big problem setting that feature up. (took around 2 hours including writing tests) - let's go to tests:
Testability:
Using WebForms we didn't really have much interest writing tests because it was a pure pain to do so.
I remember writing some tests to have at least some proofs but let's drop that topic.. (:p)
We had tests for our custom classes but we couldn't really test all the EventHandlers within the WebForms.
Our CodeBase is way cleaner and saver to use thanks to this testable environment. I just check out some of the code, modify it, run all the tests and: Oh, it broke on strange behavior! - Let's fix that again. Earlier, I remember sitting with my co-worker debugging and trying to find those bugs for hours.
With ASP.NET MVC2 we are now lacking tests!
We ask all kinds of people (even the non-Web ones) to provide test-cases we could feed into our TestSuite.
And yeah, there are some AJAX-Controls too:
AJAXability:
You asked about AJAX Controls and in conjunction with ASP.NET MVC I highly recommend you to check out Telerik ASP.NET MVC UI Controls.
If that isn't something for you, we also make extensive use of jQuery and jQuery UI
With ASP.NET MVC and the HTML Views, those libraries aren't just a pleasure to use, they just look amazingly beautiful.
There is no random-html-tag-id-value autogeneration anymore!
But what I like most is: You can finally really re-use your code again.
There is so much more to those frameworks than just that, there is the T4 templating system. Auto-Scaffolding for your ViewModels / DomainModels with the Html.EditorFor() method and of course there is a great and easy way to use the IoC and DI paradigms.
Assuming that you have asked the question with mostly .NET Framework related tags, you'll probably stick with it.
Just to keep the post complete, there are also other frameworks that are just as good (or even better):
Ruby on Rails
Django
CakePHP
And many many more!
There's also DynamicData which may be appropriate if you need simple CRUD access to your data.
The Web Service Software Factory (WSSF) might come in handy in your situation.
This will allow you to define your contracts (XML entity returned (if XML you choose), etc.) while designing the server/client communication using WCF (or standard Web Service communication protocol).
WSSF favors either ASP.NET MVC or ASP.NET MVP. A simple example of the MVP architecture is shown here, plus this article.
As for me, I often come with a hybrid-like architecture using a bit of both MVC and MVP, as both have different strong points which combined together fill each other's improvement points.
I'd also recommend looking into Silverlight.
http://www.silverlight.net/learn/
Just my opinion to use MVC on Client sites and WebForms inside administration pages(site)

Categories