How to load HTML page in MVC 5 for Angularjs routing? - asp.net-mvc

I want to use the SPA concept of angularjs in MVC 5, but cant handle the routing. I want my main page to view in Razor and load portion of the page using angularjs.
I know my description is not sufficient but hope anyone will help me through.
Thanks in advance.

Look at the project template AngularStart
https://visualstudiogallery.msdn.microsoft.com/cc6c9c5f-2846-4822-899f-a6c295cd4f2b
I am author of this template and it shows how can client side routing and mvc routing can be combined. This has been created for MVC 4, but you will get an idea.
I also created a blog post http://blog.technovert.com/2013/12/setting-up-angularjs-for-asp-net-mvc-n-webapi-project/ explaining the approach.

You may run into conflict if routing is setup both for MVC 5 and Angular. For SPA portion use only Angular routing. That being said you can still have a hybrid solution for partials where angular templates are assembled by Razor templates (which is a very powerful feature).

Related

Net5 razor pages vs mvc

I am new to net5 framework and have some questions regarding the structure of the project.
When we create a new project it will only create the razor pages. I am learning that we can achieve things with these razor pages.
Meanwhile, I have looked into some open source net5 projects on GitHub and see that they are also using MVC.
So, my question is why do we need MVC then? If everything is achieved using razor pages then what is the need for MVC?
If we need MVC then should we use razor pages with them too?
I have done the identity work with razor pages using scaffolding. What would happen now if I add MVC to my project. How will I manage the routes and page redirection from razor pages to MVC and vice versa?
Please help me with this so I can clear my concepts on this.
I will be really thankful to those who explain the scenario to me.
Thanks
First, .NET 5 is out of support. You should target .NET 6 instead.
Razor Pages represents a simpler way to generate HTML on the server compared to MVC. It is recommended for all new web applications that rely on server-side HTML generation going forward. MVC is still available for existing apps. It is also probably easier to migrate older MVC 5 (.NET Framework) apps to ASP.NET Core MVC when porting to .NET Core.
Razor Pages is built on top of the MVC framework and depends on a lot of the features of MVC such as model binding, action results and so on.
If you are using Razor Pages, there is no compelling reason to also use MVC in the same project, although you can. If you do, then you need to register controller endpoints:
app.MapControllers();
You might need to add controllers to your project if you want to create HTTP service APIs, although you can use the minimal API feature that was added to .NET 6 instead:
https://www.mikesdotnetting.com/article/358/using-minimal-apis-in-asp-net-core-razor-pages
Razor Pages can make coding page-focused scenarios easier and more
productive than using controllers and views.
https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-6.0&tabs=visual-studio
As another difference, in MVC, you usually create separate class for ViewModel. As I understand, in Razor Page, you don't need a separate class and you just add more properties in the same page and those properties will be part of your model which make things easier.

Architectural advice for a SPA ASP.NET application

I'm starting a new project, based on ASP.NET. The application will be a registry with a web interface. It will be used by ca 3000 users.
It has been ca 4 years since I last started a new project so I'm a "bit" rusty when it comes to the latest trends..
I wish the new application to be SPA so I investigated some Javascript MV* frameworks and finally settled on Durandal, Bootstrap (for layout) and Telerik's Kendo UI with ASP.NET MVC helpers.
I installed a template called Hot Towel, which created a ASP.NET MVC based Durandal project setup. However, I see that Views are html not Razor's cshtml. Can I still use cshtml files (for helper classes to work)? Is there any downfall in this?
Also I heared a discussion that when using Durandal, one shouldn't use ASP.NET MVC but rather just Web API. What do you think of that? I'd still like to use MVC helpers here and there..or is there a good reason for abandoning it for Web API?
Do you have any other considerations and recommendations on a new project setup?
Best wishes,
Andrew
Someone who knows a more about durandal than I may be able to shed some light on the part of using cshtml views. I would imagine that you would have to override the viewLocator and point it to controller actions rendering partial views??? (you need them to be parsed by the Razor engine to get valid HTML).
As for using regular html files, I have used a fantastic library called knockout-kendo
for using kendo UI components in SPA applications. I find it just as easy to use as the HTML helpers, all while keeping the application a true SPA, and you then serve up all of your data via WebAPI controllers.Hope this is of some help.
EDIT: Perhaps you can also have a look at this answer How can I use cshtml files with Durandal?. There are a few answers there that seem to be doing what you are looking to do..

How to develop Breadcrumbs for a single page Backbone.js based ASP.NET MVC Application

Im working on a single page ASP.NET MVC application. We are using Backbone.js + JQuery for client side UI and design. I need to put breadcrumbs for Backbone views(which are similar to pages in a forms based application). How to achieve this?
Thanks.
I think you can just use backbone. It does not matter if you're using ASP on the server. There are plenty of ways to do it. You can place a property in your views that relates to the breadcrumb and then just use it when you render.
If your urls are easily readable you can just parse the url and use that.

Incorporating MVC functionality in an existing ASP.net project

We have an intranet system developed using asp.net webforms. We are kind of planning to partly incorporate mvc and such a scenario is as follows,
We would like to generate html documents using mvc (using razor view engine) where the inputs are going to come from a normal asp.net webform (from a form button click or so)
Some pointers or references to tutorials on running these 2 scenarios side by side would help a lot.
ASP.Net MVC and WebForms postbacks don't mix; the only way to do this would be to use <iframe>s (which is not a good idea).
I am not sure about your described implementation... however, you could start by re-writing some of your aspx pages using MVC views and just get all the existing code behind into a controller to minimize the creation of new code. You could do this one page at a time until all of your pages are MVC views. Good luck.

New MVC project

I just need some guidance. I'm fairly new to MVC3 and to web development in general. I want to create my own project that will involve data storage/access (SQL server db) and user log in functionality. I'm not sure were to start, there seem to be so many ways for this and following tutorials doesn't really give the experience of setting up real life project (or its just my impression). Would you guys suggest starting from an empty MVC3 project, or perhaps use the scaffolding feature and build on top of it and use the membership provider built into it ? Some tutorials (by Scott Hanselman) actually explain how to copy and paste the membership feature into a new project. Is this approach suggested ? Any suggestions will help! Thanks!
Here are some tutorials
MVC 3 with razor
ASP.Net website
MVC Music Store
Strongly typed helpers in mvc
Brad Wilson's Blog
MVC Subdomain routing
MVC Partial Views
Some javascript for MVC
MVC Javascript frameworks
JSON and MVC
Form update using JSON in MVC
I'm just starting too, I found this very helpful, especially the videos on the left.
But what I found extremely helpful is the MVC Music Store and the Contoso University site (I recommend to download as PDF; you can see a button to do so at the top).
The NerdDinner tutorial is a great way to start with ASP.NET MVC, although the online version is slightly out of date. However, I can strongly recommend the book that this tutorial (in its updated version) is the first chapter of: Professional ASP.NET MVC 3.

Resources