MVC3 - How do I route to a subsite? - asp.net-mvc-3

I have a subsite within my main site for site Administration. The site is physically stored in the form
~/Views/Administration/ViewName/Index
With controllers inside
~/Controllers/Admin/ControllerName
I am getting an exception trying to visit the page.
The view 'index' or its master was not found or no view engine
supports the searched locations. The following locations were
searched: ~/Views/ViewName/index.aspx ~/Views/ViewName/index.ascx
~/Views/Shared/index.aspx ~/Views/Shared/index.ascx
~/Views/ViewName/index.cshtml ~/Views/ViewName/index.vbhtml
~/Views/Shared/index.cshtml ~/Views/Shared/index.vbhtml
I added a route
routes.MapRoute(
"Administration", // Route name
"Administration/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administration", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Going directly to the page manually
http://localhost:999/Administration/BaseItem/index
Does not result in exception, but I get no content. This leads me to believe it is not finding the View. What am I doing wrong?
I think the issue is that I have told the route engine how to route to the Controller, but I have not told the system how to route to the View. Where do I tell the system where the views are?

The problem is not with the routes, but with the design. The view engine cannot find your view, because it cannot find the correct path, since the default view engines are not designed to search for a subsite.
Instead of creating a subsite, make Administration an Area in your project. In AdministrationAreaRegistration.cs, you will set a route similar to the route you added. Place your views in the Views folder inside the Administration folder (inside the Area folder), and everything will work properly.

Related

Handling multiple views controllers in CodeIgniter 4

I am new to CodeIgniter 4.
I have one controller named Pages and helps to display website pages normally. Then I have another controller named Admin which is suppose to load the admin view with its own header and footer files.
While Pages controller works fine, the Admin controller keeps saying 404-file not found admin.
I think I have a problem with my current Route settings below. Please help.
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('default');
// Route Definitions
$routes->get('/', 'Pages::index');
$routes->get('(:any)', 'Pages::default/$1');
try ajusting your code like this.
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('default');
// Route Definitions
$routes->resource('pages');
$routes->get('/', 'Pages::index');
$routes->get('(:any)', 'Pages::default/$1');

Routing problems, CodeIgniter

This might be a very silly question because I'm new to PHP frameworks. But I have got the concept of MVC framework. But this routing thing is very confusing me.
I created a controller and view for dashboard and set it as default. Then I wanted to check if the user is logged in then send him to the login page if it's not.
So, what I did in the routes is this:
$route['default_controller'] = 'dashboard/index';
$route['login'] = 'login';
But even if I add a thousand more controllers and routes, it still goes to the default i.e the dashboard. This was supposed to work http://localhost/codeigniter/login. I'm very sure I haven't put a redirect-to-dashboard code in the login page.
at first it's important to understand to routing in codeigniter.
The key for understanding is $route['url/path'] = controller/method (or "public function")
So. In your case, you routing to Login controller and index method (or index function...this it interpretation of codeigniter )
If you want to check if is user logged or not, you may try to check for examle session variable inside of called functon. But it depends on your application. But I think that is not good idea to check it in routes.php
Hope it helps you.
The default controller in routes, will be the controller that opens your index or first page.
If your first page is in controller "pages", then that is your default controller. Do not use it for your secure dashboard pages
Your first page should be the index method in the controller. It should look like this
$route['default_controller'] = "pages";
Where pages is your first page controller

Loading an MVC site

Hello I am in desperate need of help. I just created an MVC site made in Visual Web Developer 2010
and each time and on different Web Hosts I get --Index of /-- instead of the site loading properly. It's as if the server or browser is looking for an 'index' file instead of being routed through the MVC folders to the proper start up page. I've done everything I believe I am suppose to do as far bring all my dll 'system' files to the 'bin' folder as well as everything else including getting a Web Host that has MVC supporting server but I still keep getting 'Index of /' and the folders instead of the site. Can anyone help? I'm really in a spot. I've been working on this site for months and I need to get it up and running.
Thanks, Rob
It looks like your default route is not setup or the setup is pointed to a place that doesn't exist. Your default route will be in the default.asax.cs page (MVC4 is App_Start/RouteConfig.cs) and looks something like this:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
So the the Default action is Home/Index. You can change this if you want to redirect to a different default page.

deploying asp.net mvc 3 website

I'm trying to deploy my finished website to the hosting company server and i'm basically stuck. I have wwwwroot folder on the server where I put my folder of the published project. So what is the index file, the first page that I should display.. is it the layout page or simply my index.cshtml or maybe the _ViewStart? I got to create a path to the folder where the file would be located and give them the name of the "index page". I've been trying to do this for a while now with no luck.
Your default route should go to the default controller/view (ie /home/index)
So if someone visits your site at www.yoursitewhatever.com it will automatically find the default page.
Here is a link that might help you and explain the differences between previous MVC versions .
http://haacked.com/archive/2011/05/25/bin-deploying-asp-net-mvc-3.aspx
A default project creates the default page in the global.asax file that is why by default it is located in home/index folder as in
routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults );

MVC3 start page

I am working on a web app in Visual Studio 2010, its mvc3. I Was trying to figure out how to publish it, and through various instructions I tried setting View/Home/Index as the start page. This was a bad idea. Now nothing is working, even trying to view the site as I have been (debugging with F5) is not loading properly.
I don't know what the start page was before, or how to undo this. I am very new to web development, and a little lost right now. How do I get my start page back?
Go to the project's Properties
Go to the Web tab
Select the Specific Page radio button
Remove url in the Specific Page text box
Save Properties Tab.
Try setting the start page to /.
If you leave it alone after set up, it should be like so:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
My suggestion is leave it like that. How do you "get back"?
Simple: Open another instance of Visual Studio. Create a blank MVC3 project. Copy over the routing rules and add custom rules (if any) below. Copy over the web.config with any customizations you have made (if any).
Then, right click the project and select publish. If you publish to a local folder, you will have to set up IIS properly, but you can work through the kinks on your local IIS instance (assume you have installed it, as this is a developer machine?). Once you know the set up, you can move to the server and you should be fine.
That is about all of the time I have for this one right now. Good luck!
i know this is an old post... but whoever comes after this would find the piece of info helpful
if you want to change the start up page in MVC3 u can do so by just specifying like this
Specify Page: Account/Logon
No need to include the view name if you say Views/Account/LogOn it would return Resource not found error and if you include / it would say bad request.
I tried the above option and could change my start up page from the annoying index page to LogOn.cshtml
Simply change your starting url to /Home/Index or / (or remove it) - you cannot address items directly in the /Views folder.
Try creating a new html file in the main project and set it as the default. Then delete the file. That should remove the start page. You do not need one for Mvc.
Right click on project --> Properties
Go to the Web tab
Select the Specific Page radio button
Specify the start page as ControllerName/ActionName as it comes in the browser url when the page is loaded
Save.

Resources