Use Anchor Link in asp.net MVC - asp.net-mvc

I put an html anchor link in one of my Views. Just like this:
<a href="SomePage.html"> Some Link<a>
It does not go to the page. I suppose it is because it does not have a Controller and is not wired up with MapRoute.
My question is: Can I use a regular html anchor tab in an MVC application or Must I wire it up with the MVC Controller and MapRoute?

This is because an HTML file in a view folder doesn't automatically have an action associated with it. Remember, with MVC, views are just something that is rendered by a controller that is serving a route. The views folder is just a handy place to store these views, and doesn't map to any actual URL's unless there is a route specified.
The general convention is to put these kinds of files in the Static or Content folder, where they will be served up just fine.

You should be able to. Is the path correct? What page is it on? Are you going up the path tree?

Related

MVC .NET Core 2.0 - _Layout static files do not load for action methods of a controller

I started a new project in VS 2017 and created a ASP.NET Core 2.0 web application (Model View Controller). Then I've changed the conent of the _Layout.cshtml to the interface I want to use (included #Renderbody etc.) and included all the static content it requires in the wwwroot and save and Ctrl+F5 and the layout shows with all the correct formatting and functionality. No problem so far:
However from this point forward none of the static content files of the _Layout shows in any other view. So for example if I visit any of the following URLS:
http://localhost:52786/home/
http://localhost:52786/home/index
http://localhost:52786/home/about
http://localhost:52786/home/contact
I see this:
Startup.cs already has app.UseStaticFiles() in Configure()
So it sounds like you may have your HTML body content in the wrong place. With ASP.NET MVC &ASP.NET Core MVC, the HTML for each page is served up from the /Views directory (by the Home controller by default), not from the /wwwroot, and it is C# HTML (.cshtml) just like the _Layout.cshtml file. Things like images, static javascript etc. are what's typically located in the /wwwroot directory as these are the parts of your website that are client side instead of server-side.
Try replicating your index, about and contact HTML content as .cshtml files under the /Views/Home directory, replacing whatever's there in the template, except for #{ViewData["Title"] = "Home";} located at the top. The layout template should then serve these as the body content (where #renderbody... is in _Layout.cshtml).
If this still doesn't work I'll need a bit more info about what you've changed from the default template. Hope this helps!
Thank you James for your response. I figured it out and it's actually a noob mistake which I'm posting so that other new developers don't get caught in it.
When you want to reference the content in the wwwroot folder if you reference them without "~/" before the name of the directory it works for the _Layout page which fooled me to think it should work for other parts of the application but you should include "~/" before the folder names explicitly otherwise the static files will be un reachable.

AngularJS + ASP.NET MVC: routing URLs without corresponding physical view

Context
We have a web app using AngularJS with ASP.NET MVC. It aims to replicate a file-system structure through URLs, like this:
http://(site1)//rootFolder//NFolders//File
For views in Angular, we are using stateProvider, with different states associated to existing views in disk (.cshtml files).
The problem issues when we request a file or folder from the browser and we try to serve a virtual view that doesn't really exist. We have just been able to achieve it with a full postback and some hacks, making a complete reload on each file/folder request.
Question: Is there a way of achieving that in Angular? Other way, the request for different URLs which don't have either their corresponding physical view as a file or any state in stateProvider, handling them with Angular and customizing a specific view which would deal with disk routes requested by URL?
RE-EDIT
We will have:
a fixed root view for the website: http://website
several root projects with a name:
http://website/site1
http://website/site2
....
http://website/siteN
each site may have different files and folders accesible via the URL:
http://website/site1/folder1/folder2/.../foldern
http://website/site1/folder1/folder2/.../foldern/fileX
RE-QUESTION: is it possible to handle this "virtual" URLs without having a physical view behind for folders or files? Our idea is to have one unique view called siteController which would control all the sites, and paths to folders and files.
I would suggest you define rootFolder/NFolders/File as parameters. Then you can have a html file as the view template to populate data from server.
Have a look at this link
https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

ASP.NET MVC: how to bypass the controller in MVC app?

I have a .NET MVC application. So far, each page is accessed via a controller.
Now I want to direct access some cshtml files such as
http://example.org/file/abc.cshtml.
Though having .cshtml file extension, these are just pure html snippets.
How can I access these files without going through any controller.
Thanks and regards.
I would probably make a controller with an action which accepts a view name, grabs the view from the file system, and returns it as a FileResult with the mimetype text/html. You'd probably want the controller to have a hardcoded whitelist of html-fragment files, to reduce the chances that you're opening up a way for people to browse around your folder structure.
You could also look into configuring IIS to serve .cshtml files from some directories, but I'd be more concerned about accidentally opening up too much using that method.

Ruby on Rails ajax - dynamically loading all pages

I have a flash media player (similar to lala.com) that needs to continue to stream while people click around. I don't want to use an iframe.
So, I need to dynamically load all site pages with ajax no matter what link people click on.
I've got this working with Rails and JQuery for a single page. With this method I have to place a file.js.erb file for whatever controller is called. Example: example.com/home is called and I have to have an index.js.erb in the views home dir to respond to this.
I used:
http://railscasts.com/episodes/174-pagination-with-ajax
to get this to work on one page, but it wouldn't be DRY at all to copy .js.erb files to every controller.
Is there something I can do with the main application_controller or even with routes.rb?
I found a way to do this. You can load whatever pages you want via the jquery load method.
$('#result').load('ajax/test.html #container');
They will then be placed in the div with the #result id.
You can also specify a page fragment with #container so you only load the part you want.
This allows all your controllers to stay in tact etc with no extra js.
This is really going to impact your visibility to search engines. I also think it will mean you need to think about the flow of your controllers. Rather than having controllers responding with views that represent a page, controllers will respond with snippets of HTML/JSON/etc that get injected into the home page. Your home page acts as a coordinator, loading the content in and out. Perfectly fine to have js.erb files in each view folder ... as they should be returning content specific to an individual controller.

How to access HTML files from ASP.NET MVC VIEWS Folder

I will like add conventional HTML page under VIEWS folder (in ASp.NET MVC) page.
I have added the route exceptions as mentioned below.
routes.IgnoreRoute("{resource}.htm/{*pathInfo}")
routes.IgnoreRoute("{resource}.html/{*pathInfo}")
Although it does work when I put the html files out of VIEWS folder but I get Page not found 404 when I put those in VIEWS folder. I am also unable to browse the VIEWS folder by setting directory browsing option in IIS.
Please help me on HOW to access HTML file from VIEWS folder.
I think that it's a mistake to mix your HTML content with your views. I'd suggest that you create a separate static folder under Content and put your HTML there. You can create an analogous directory structure to your view structure if necessary for management. Then you don't need to do anything special in order to able to reference the files. You can even, then, open them up to editing with Contribute, etc. by people who are allowed to modify static content.
+-Content
+-Images
+-Static
+-Account
+-privacy.html
+-refunds.html
+-Styles
Usage:
<a href='<%= Url.Content( "~/Content/Static/Account/privacy.html" ) %>'>Privacy Policy</a>
The default Views folder has an Web.config file that explicitly gives 404 errors for all requests. You just need to edit and enable for HTML files (or all files, but then people might snoop).

Resources