How do I redirect to the cshtml page created inside shared folder in ASP.NET MVC3 - asp.net

Please help me out how do I redirect to a .cshtml page which is created inside a shared folder.
I want something like this
href="#Url.Content("Shared/UnderConstruction")"
Here it's not getting redirecting to the UnderConstruction page, which I created.

You cannot redirect to anything that's stored inside the ~/Views folder including the ~/Views/Shared. This folder is simply not served by ASP.NET MVC and cannot be accessed directly. Also notice that in ASP.NET MVC you are not serving .cshtml pages directly. You are always passing through controller action that return views and those views might represent .cshtml pages.
Now if you have some Shared folder directly under the site root ~/Shared, then you can do this:
construction

In ASP.NET MVC3 you can't render views directly by calling the files directly. They can only be served via controllers.
In order to call the view in your shared folder you woul have to do something similar to the following:
public class HomeController : Controller
{
public ActionResult About()
{
return View("Construction");
}
}

If you want to display a page at url "shared/underconstruction" as per the other posts:
Create controller SharedController.
Define action "UnderConstruction"
Create "UnderConstruction.cshtml" in Views/Shared/ folder.
Map URL "Shared/{action}" , new { Controller = "Shared" } if you want to be explicit.
Give that a shot... to be honest even I don't know if this will work, and you will pollute your "Shared" folder. You could rename existing Shared folder to something else, maybe, and modify ViewStart.cshtml to point to new folder, maybe.

In ASP.NET MVC you can only redirect to controllers, and the controllers return a view. You can access views in Shared the same way as your normal controller views, by their name. ASP.NET MVC first looks in your controller view folder then in your shared view folder when resolving view names.

Related

IIS server error .Server Error in '/' Application

This is an ASP.NET MVC app but using the Web Forms View Engine (.aspx) instead of the Razor View Engine (.cshtml).
I've set the default start page to Login.aspx in the project and in IIS default document. However if i enter http://localhost:150/Login.aspx to the web browser it works. What am I doing wrong?
If you want to access to the route '/' you should have a HomeController. Inside this controller, put an Index action that returns View(). Then create a Index.aspx file in the Views/Home folder. If you want access to other view when you enter to your site with route '/', then you should change your RouteConfig file, and set the default controller and action to others.
I have figured it out. I moved my login page into the VIEWS folder ( it wasn't before).
Created a default controller
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
return Redirect("~/Login.aspx");
}
}
And not everything seems to work. Thank you for your assistance.

web api spa the view at '~/index.cshtml' must derive from WebViewPage, or WebViewPage<TModel>

Hi Using John Papas codecamper as an example, My web api spa has index.cshtml at the root folder with a few cshtml pages in a sub folder called views. There is no web.config in the views folder
When I put
#RenderPage("Views/welcome.cshtml")*
in the index.cshtml I get error
"The view at '~/index.cshtml' must derive from WebViewPage, or WebViewPage."
If I don't have renderpage then the index.cshtml loads ok. What am I missing? Does the codecamper require Require.js or something to use #RenderPage. I am not routing index through a controller and don't want to .
Sorry am aware this is a common problem but can't get it to render using any suggestions on web.

Where is the mapping of a Controller to a View in ASP.NET MVC

I am new to ASP.Net programming.
I have created an empty MVC controller called TestController, and as I follow the tutorial it says right click on the Index method of the Controller , then click Add View.
The View is successfully created and with I open the page
http://localhost:9993/Test
It successfully opens the relevant view file (Views->Test->Index.cshtml)
My question is where exactly in the code is the mapping defined that relates a View to a Controller ? Because when I open the controller, it has no information about which View file it relates to and Vice versa
In RouteConfig.cs (App_Start folder), your routes are defined. You can add or customize them there, in order to get to the right action in the right controller.
About views, it works by convention : if a controller is named TestController, and the action is named Index, it will search for an Index.cshtml view in a Views\Test folder (or Views\Shared if you wish to reuse a view across multiple controllers)
By default ASP.NET MVC searches appropriate views in {controller} subfolder of Views folder, where {controller} is a name of ASP.NET MVC Controller class without word "Controller", and in Views/Shared folder.

Having ASPX, HTM and MVC views in the same folder

We liked the approach to have all the pages regarding support - for example - under http://www.company.com/support. After migrating to ASP.NET MVC 3 and trying this we can run every type of page but not inside the same folder.
Is there any workaround for this?
Thanks.
If you need to mix MVC pages and non-MVC pages in the same folder, here's some tips:
Remove the default route "/{controller}/{action}/{id}" and make routes for each MVC page. That way any request that isn't caught by a route falls through to the "old" request handling.
A return View(); method call in a controller looks for a view in a folder named as the controller in the Views folder, so specify the name of the view, e.g. return View("/support/index");.
Note that the MVC views doesn't actually have to be in the folder support, you can put them anywhere you like, it's the routes that determine which URLs are handled by MVC.

protecting files with asp.net (mvc)

I want to protect the files in a specific directory so that people cannot link to them - but I still need my program to be capable of rendering them, so password protecting the actual folder won't work. Is there any way to facilitate this in ASP.NET MVC? Obviously it's more about ASP.NET, not MVC - but I can't make use of things like Session State and Postback and such...
You could put that directory outside of the web app's root directory (so that it can't be accessed using some copied URL) or into a directory where you deny any read access using a web.config file.
Then access the files through an action which requires the current user to be authorized, e.g:
public class FileController : Controller
{
[Authorize]
public ActionResult Get(string file)
{
return new File(Path.Combine(_rootPath, file);
}
}
Then you can access the files through an action URL, e.g. http://server/app/File/Get/test.txt.

Resources