asp.net mvc 5 can i hide controller name from url - asp.net

we have one project in asp.net mvc 5 when i run my project and click on some button for go to the next page then we are getting the url like
wwww.abx.com/project/projectname .
where project is my controller name and projectname is Action name
so if i want so url like
wwww.abx.com/projectname
then this is possible ?
please if any one have some solution so please give it
here is my route.config file code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "view1", id = UrlParameter.Optional }
);
}
Thanks

you have to create a new route:
public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ProjectName1",
url: "Projectname1",
defaults: new { controller = "Project1", action = "Projectname1" }
);
routes.MapRoute(
name: "ProjectName2",
url: "Projectname2",
defaults: new { controller = "Project2", action = "Projectname2" }
);
routes.MapRoute(
name: "ProjectName3",
url: "Projectname3",
defaults: new { controller = "Project3", action = "Projectname3" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "view1", id =
UrlParameter.Optional }
);
}

If you would like to do this for only one controller - add a custom route as suggested by #Hamed Javaheri, or use attribute mapping as described here
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Project",
url: "projectname",
defaults: new { controller = "project", action = "projectname", id = UrlParameter.Optional }
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
If you want this to work for many / most routes then you might want to change the default mapping, somethin similar to this question:
ASP.NET MVC Default URL View and
ASP.NET MVC Routing with Default Controller
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{action}",
defaults: new { controller = "project", id = "" }
);
}

Related

How to pass string as parameter in ASP.Net MVC

I am trying to make a simple sample of passing string parameter in URL not using the queryString at this example. First of all I added a new MapRouteto RouteConfig.cs file as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}"
);
and in Controller I have
public class AppController : Controller
{
public string Index(string name)
{
return HttpUtility.HtmlEncode("Hello " + name );
}
}
but the view is not displaying the string parameter. For example a URL like this http://localhost:59013/App/Index/Ali only return Hello!
Why is this happening?
Update
routes.MapRoute(
name: "app",
url: "{controller}/{action}/{name}",
defaults: new {action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
First, you need to change the order of your registered routes so the default will be the last.
Secondly, your app route pattern is wrong and will always collide with the default route. You better change its pattern to
url: "App/Index/{name}"
Or perhaps to the more friendlier
url: "App/{name}"
Both with
defaults: new { controller = "App", action = "Index" }
So that your routes would look like:
routes.MapRoute(
name: "app",
url: "App/{name}",
defaults: new { controller = "App", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
See MSDN

Default-Route for root of application

How can I tell my mvc-application to route to a specific Controller and Action when they are not specified?
When debugging http://localhost:54500/ should route to http://localhost:54500/Home/Index.
Currently I have:
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
But this always throws
The view 'Index' or its master was not found or no view engine supports the searched locations
Edit #1
It should redirect/route to an View which resides in a Area called Home. Just want to clarify, that there is a Controller and an Area which both are named Home.
The config for the area is:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
When debugging http://localhost:54500/ should route to http://localhost:54500/Home/Index.
Actually, the way you have it configured, http://localhost:54500/ will route to the HomeController.Index method, not another URL.
The view 'Index' or its master was not found or no view engine supports the searched locations
This error indicates that routing succeeded, but the controller returned the path of a view that does not exist.
Since you also mentioned you are using an Area and have posted your configuration, it is clear what is happening. Your config is run in this order:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
So, if you pass the URL http://localhost:54500/, the Area route will miss (because it doesn't start with /Home) and it will match the Root route. This Root route does not route to your Area. There are 2 ways to fix this.
Option 1 - Add the Root Route to the Home Area
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index" }
);
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Option 2 - Set the DataToken to indicate the Home Area
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Home_default",
"Home/{controller}/{action}/{id}",
new {action = "Index", id = UrlParameter.Optional}
);
}
routes.MapRoute(
name: "Root",
url: "",
defaults: new { controller = "Home", action = "Index" }
).DataTokens["area"] = "Home";
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
In Core 1.0.1 you can do this in Startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc(routes =>
{
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
// template: "{controller=Home}/{action=Index}");
template: "{area=MyArea}/{controller=Home}/{action=Index}");
});
}

Azure MVC Angular Project not finding Home page

Locally the website works fine, when deployed to azure we get the view index or its master was not found or no view engine supports the searched locations.
Routeconfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "websitename",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "{controller}", action = "{action}", id = UrlParameter.Optional }
);
We are using angularjs and having no problems until deployed to azure other than this issue.
You define following for home page right?
routes.MapRoute(
name: "DoransData",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Change it to
routes.MapRoute(
name: "DoransData",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Based on your code I think that you could only need following code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

ASP MVC Route not firing

I am having a bit of trouble getting my routing working correctly. I am needing to support IIS 5.1, so I want to suffix the controller with .mvc, here is what I have currently:
routes.MapRoute(
name: "Default",
url: "{controller}.mvc/{action}",
defaults: new { controller = "App", action = "Home" }
);
This fails. If I remove the .mvc, it works as expected. Also, weirdly enough if I add another path in between the controller and action, it works. Like this:
routes.MapRoute(
name: "Default",
url: "{controller}.mvc/Anything/{action}",
defaults: new { controller = "App", action = "Home" }
);
Does anyone know the reason for this?
Thanks in advance,
Ryan.
I have tried with code as below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "DefaultWithExtension",
url: "{controller}.mvc/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
It works correctly. Please try those code in your application.
IIS could be treating the request as a static file or another handler is receiving and processing the request.
If this is the case, you'll need to add a handler mapping for the .mvc extension in IIS and ensure it's using ASP.NET.

ASP.NET WebPages Routing

I created simple asp.net project.
Have folder Account, and many files like Register.cshtml, LogIn, etc...
And url: localhost/Account/Register
Want it to be: localhost/Register, localhost/LogIn.
Tried to map it in _AppStart.cshtml:
RouteTable.Routes.MapPageRoute("Account", "{Action}", "~/Account/{Action}");
And it doesn't work.
First of all you have to use MapRoute to MapPageRoute, because MapPageRoute is (from msdn):
Provides a way o define routes for Web Forms applications
But if you modify your code like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("AccountRoute", "{Action}", new {controller = "Account"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Every request without parameters will go to AccountRoute, even if it should be handled by other controler.
The only way it will be working is to create code like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ALL routes you need
routes.MapRoute("LoginRoute", "{Action}", new { controller = "Account", action = "Login" });
routes.MapRoute("RegisterRoute", "{Action}", new { controller = "Account", action = "Register" });
//Default route in the end
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Try this one....
//Default
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"",
new { controller = "Home", action = "Index", id = "" }
);
//url rewriting
RouteTable.Routes.MapRoute(null, "Register/{ID}", new { controller = "Home", action = "Register" });

Resources