I want to start a new AngularJS single page application in ASP.NET 5 (Visual Studio 2015). I will only be using ASP.NET for its WebAPI functionality and not for view rendering (Razor).
Therefore I'd prefer it if I didnt have to have a HomeController and _Layout.cshtml from which to bootstrap my Angular app.
Is there a way to remove this and just get ASP.NET to return a static initial page? I will do all my routing via AngularJS
Update
As per #Phills suggestion I have put index.html into wwwroot. If I navigate to http://localhost:62583/index.html it works but does not if I navigate to http://localhost:62583/
Yes, you can do this. Place your file inside wwwroot/ (e.g. called index.html), and then ensure the following is present in your Startup.cs Configure method:
app.UseDefaultFiles(); // Allows index.html to be used automatically
app.UseStaticFiles(); // Must be AFTER UseDefaultFiles
app.UseMvc(); // Required for your API controllers
Incidentally, _Layout.cshtml is not required even if you stick with using HomeController - you just need to remove the reference to it from Views\Shared_ViewStart.cshtml. If you went this way, you would place all of your content in Views\Home\Index.cshtml.
You can create an index.html page in the root of your app, and remove the HomeController, if no route is found it will pick up the index.html page instead.
Yes you can. Your views don't necessarily have to have a layout page. You will still be returning a cshtml view from your controller, which will basically be a static HTML that starts your Angular app.
No you don't especially since you are going to develop a Angular JS application you will be able to handle templating using AngularJS itself.
Instead of layout page you can have index.html and you can use <ng-view/> tag you render partial views.
You need to have some routing setup similar to MVC
angular.module("myApp",["ngRoute"])
.config(function($routeProvider){
$routeProvider.when("/home",{
templateUrl:"/views/home.html"
});
$routeProvider.when("/placeorder",{
templateUrl:"/views/placeOrder.html"
});
$routeProvider.otherwise({
templateUrl:"/views/home.html"
});
});
You view will look something like this
<body>
<ng-view/>
</body>
Related
I have a menu system that calls ASP.NET MVC controllers without any problem. I want to call Razor pages from the same menu structure.
The system is using default ASP.NET MVC navigation thus defaulting to /Views/etc.
How do I switch to /Pages/etc for the Razor pages?
I tried setting the path to the page like this
./Pages or ../Pages or ~/pages
I have tried to use
return RedirectToPage("../Pages/Etc");
in the controller to call the Razor page. Again same issue with the default ASP.NET MVC navigation being imposed.
The value passed in to the RedirectToPageResult methods is relative to the current page, unless it is prefixed with a forward slash (/) in which case it is relative to the Pages folder (https://www.learnrazorpages.com/razor-pages/action-results)
The Pages folder name should not be included in that value. So if you wanted to redirect to a page named About, located in the Pages folder, you just pass in the name of the page:
return RedirectToPage("/About");
I am facing a problem that is driving me crazy. I will try to be simple about the issue.
I have a project that uses MVC and Angular. The project has a page layout to signin and another to use in main project. I can't change the way the users access the pages, so I need to create the routes the same way they are using.
I want to change the mvc route to use different actions to use the correct layout, one is for signin and the other layout to the main app. The examples below:
To access the signin page: {mainurl}/#/signin
To access another signin page, but for support, using the same layout as signin: {mainurl}/#/signinsupport
To access the main page, after signin: {mainurl}/#/main
How can I change the mvc route to access these different pages using the urls above? Is there an easy way to do that?
Thanks a lot for your help!
Able to create Controller and Views on my ASP.NET MVC app.
When I start the application each time I have to type /index.
I read through posts which suggests to use routes.MapRoute(..
But my Global.asax file does not contain route directly.
Do I need to use RouteTable.Routes.MapRoute(.. instead.
In MVC4 the default routes where moved from:
Global.asax
To:
App_Start/RouteConfig.cs
I have Asp.net application.
I want to integrate another Asp.net MVC application to exsting Asp.net application.
So I did the following changes.
1.I added area in exsting Asp.net application.
2.Added path in routeconfig.
3.Now How to call Asp.net MVC Razor ??
Controller Name-Home
Action Name-Home
How to call Home page inside MVC Area from js file present in asp.net project.
Thanks in advance !
it's same as calling other page.. just you set up the proper routing structure in order to render the view.
document.location.href='yourArea/FolderName/Home/Action';
or you can use an HyperLink to achieve the same
MVC view
If you only want to call method, then simple way is Response.Redirect("~/ControllerName/ActionMethodName");
I have an existing Web Site that uses multiple ASP .Net Web Forms projects. I have created an MVC 4 mobile web application to replace the login/ authorization web form project.
So far I have been able to replace all the functionality of the site, except I am unable to redirect to the ASP .Net pages from inside the Controller.
Using this method I can reach other MVC applications on the same web site without issue:
return Redirect("MyURLstring");
However when I redirect to a Web Forms page I receive several JS errors, and the Page never redirects. It looks like the MVC application is attempting to apply JQuery 1.7.1 to the URL that is being opened.
I can reach the URL without error if I include a rel="external" tag within a hyperlink in a view.
I have also attempted to solve this by creating a route in my RouteConfig.cs file
RouteValueDictionary RouteValues = new RouteValueDictionary();
RouteValues["rel"] = "external";
return RedirectToRoute("MyRouteName", RouteValues);
Any help is greatly appreciated.
Solution:
I created the project as a MVC 4 Mobile site as a base, but still had the referencing to jQuery Mobile in my main _Layout view.
#Scripts.Render("~/bundles/jquery", "~/bundles/jquerymobile", "~/bundles/jquerycustom")
I kept this code intact for my _Layout.Mobile.cstml view, but changed to below for _Layout.cshtml
#Scripts.Render("~/bundles/jquery", "~/bundles/jquerycustom")
This allows me to perform a return Redirect("myURL"); from the controller without hopping through all the jQuery that was preventing the redirect.