Azure MVC Angular Project not finding Home page - asp.net

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 }
);
}

Related

Routing deletes "/Index" from URL

in my application I have a CompanyGroupDataController.cs.
When the execution gets into its main action (Index), through this URL:.../CompanyGroupData/Index, the URL not longer ends in "/Index", so it erases the last part (althoug in the browser bar it keeps it). I think the problem is in my RouteConfig.cs which is this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{company}/{controller}/{action}/{id}",
defaults: new
{
company = Skill4IT.WebApps.Admin.Helpers.Tools.AdminUrlAccessCode,
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "spa",
url: "#{controller}/{action}/{id}",
defaults: new
{
company = "admin",
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},
namespaces: new string[] { "Skill4IT.WebApps.Admin.Controllers" }
);
routes.MapRoute(
name: "Catchall",
url: "{*catchall}",
defaults: new
{
controller = "Error",
action = "Error404",
}
);
Thanks

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

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 = "" }
);
}

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}");
});
}

Asp.net Mvc Routing issue

I have controller named PartialController and have a action method named AboutMe.
I can access my action method from localhost/Partial/AboutMe . But i want to access it from localhost/About. So i changed my route like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "About",
url: "About",
defaults: new
{
controller = "Partial",
action = "AboutMe",
id = UrlParameter.Optional
}
);
But i got 404 exception when trying to access AboutMe action from localhost/About. Need advice.
Define your About Route first. Order matters when it comes to routing, they are applied in the order in which they are defined.
routes.MapRoute(
name: "About",
url: "About",
defaults: new
{
controller = "Partial",
action = "AboutMe",
id = UrlParameter.Optional
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Blog", action = "Index", id = UrlParameter.Optional }
);

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