How to hide controller and action from URL in DEFAULT route - model-view-controller

I'm using MVC and having trouble figuring out how to hide the controller and action in the URL.
I have seen many things on how to hide the controller or the action but most are not for the default controller.
My controller looks like this:
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
When you navigate to my site the URL looks like this:
www.mysite.com/Home/Index
I need it to hide the "Home" and "Index" so it looks like this:
www.mysite.com
I have the default route as the last route in the routeConfig file. Any help would be appreciated.

Is this your root URL. If so than use the following code in your routes.rb file.
root "home#index"
then you will get the www.mysite.com
However, there are many techniques to hide the controller and action from the url. Please have a look on this post rails remove controller path from the url

Related

Redirect non-existent url to home in Codeigniter

A client has seen a non-existent page show up in Google Analytics and would like it redirected to the home page. I am new to CodeIgniter.
I don't see an .htaccess file in this project in the root directory, and tried the following in the routes.ph but it did not work. Do I need to add something in routes.php?
$route['strangepage'] = 'Home';
Try This--
Please put in application->config->routes.php
$route['default_controller'] = 'home';

Route to a specific controller in codeigniter

I am using codeigniter for the back end of my website.
For that I have created admin folder inside my controller.
My issue is that when i type the url
http://localhost/varma/admin
It should redirect to the file login.php which is inside the contollers/admin
Inside the routes.php write this code:
$route['admin'] = 'admin/controller/function';
Try this code in your routes.php file ,
$route['admin'] = 'admin/login/your_login_function_name';
$route['admin']='admin/login';
where login the is login function name

Route to admin page in codeigniter

I'm working with Codeigniter and i want to have a URL like this:
admin.domain.com
And then route it to the real controller and function.
It means I don't want to have a URL like this : www.domain.com/admin.
I have one controller for back-end and another for front.
This is my default controller in route file:
$route['default_controller'] = "home";
so how should i do it? with route file or htaccess file or some another way?!

The resource cannot be found. ASP.NET MVC3

I have no idea why this error is occurring after debugging the project even though the codes are default.
Controller
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
View
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
Hi
</div>
</body>
Somehow or rather, after debugging, the Requested URL is always /Views/Home/Index.cshtml but accessing Home via browser is fine. (http://localhost:58323/home)
I googled and solution hints that the problem lies in global. But thats weird, I dont remember making any changes to it.
Global
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
);
}
Appreciate any help. Thank you
I think you just have your visual studio settings set so that the view is set to the start page. Right click on the project and go to properties, then to the web tab. Is the 'specific page' radio button selected with 'Views/Home/Index.cshtml' set as the value? Change it to use a start url. Personally I prefer not to have the debugger start up a browser and use Don't open a page.
Right click your web project -> Properties -> Web
check that you have the start action set to Specific Page with no value in the field.
My guess is that you have the start action set to current page.
This error might even cause if the hierarchy of the folder structure is incorrect in Views folder.
If you are adding views by right-click on Views folder.The new view added might be incorrectly placed in the folder hierarchy.
The way to troubleshoot the problem is:
Consider a view named index.ascx which is supposed to be linked to a Controller named HomeController.
Under the Views folder there should be a folder name Home (relating to the Controller) and the
index.ascx should reside in Home folder.
The best possible way to add view is to right-click just beside the public method which will show a Create View option in context-menu.If you crate view in such manner then folder hierarchy is automatically created.

CodeIgniter URL routing - SEO friendly URL

I've created a new route for my site:
$route['default_controller'] = "welcome";
$route['(:any)'] = "welcome/index/$1";
$route['404_override'] = '';
And this works fine in my site when URL is like this:
http://mydomain.com/first-article
http://mydomain.com/second-article
*my controller is just welcome.php
but
i also have a controller for the admin and the URL for the admin is:
http://mydomain.com/admin
What will I add to the routes file to ignore the /admin and other controllers inside the admin?
You can replace the welcome/index/$1 route with:
$route['^(?!admin).*'] = "welcome/index/$1";
This basically says, that if a URI that does not start with "admin" should route to the welcome/index method and pass the contents to the index method. Otherwise, handle normal routing with admin being the controller.
Open application/config/router.php and change the
$route['404_override'] = '';
to
$route['404_override'] = 'router/index';
You can use all controllers as the normal way.
When you try to use a controller that is not exists, you should route it to a 404 controller.
Create a controller named Router.php as a controller structured for CodeIgniter.
In the index method inside Router.php, query for related sef url and do required operations. All requests that routes to an undefined controller will be handled by router/index method. Others will be redirected to related controller as usual.
You might want to use header codes to point that related page is not 404.

Resources