Two languages on site asp.net mvc 4 - asp.net-mvc

I study a lesson which describes how to create a site with two or more languages,
and the first step in the lesson - I need to add the following code in my project
context.MapRoute(
name: "lang",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints : new { lang = #"ru|en" },
namespaces: new[] { "LessonProject.Areas.Default.Controllers" }
);
context.MapRoute(
name : "default",
url : "{controller}/{action}/{id}",
defaults : new { controller = "Home", action = "Index", id = UrlParameter.Optional, lang = "ru" },
namespaces : new [] { "LessonProject.Areas.Default.Controllers" }
);
in file DefaultAreaRegistration (/Areas/Default/DefaultAreaRegistration.cs)
but I dont have this file in my project.
I dont understand I need to create a new folder Areas and a new file DefaultAreaRegistration.cs or I need to change RouteConfig.cs file which contains
public class RouteConfig
{
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 }
);
}
}
?
And in Global.asax there is the following code
AreaRegistration.RegisterAllAreas();
What's part I need to change?

you need to add this code to DefaultAreaRegistration.cs if this area was created before.
It means that the multiculture route cannot be created automatically when area creating. So if you dont have any Areas in your solution you just need the register route in RouteConfig.cs. its alredy done.
And u dont need any Areas for multyculture functionality.

Related

Add folder to all URLs in an ASP.net MVC 5 site

I have an MVC 5 site with this structure:
example.com
example.com/area1 (index for whole section)
example.com/area2/item5 (specific item for section)
For SEO reasons I'd like to change every URL to this:
example.com/keyword (redirected from example.com)
example.com/keyword/area1
example.com/keyword/area2/item5
The keyword is fixed and always the same, though at some point in the future there may be a site with the same structure but different content with a different keyword in. That is probably at least a few months off though.
What is the quickest / easiest way to implement the above. Being able to get the name of keyword would be an advantage later though isn't essential right now.
I could use attribute routing, though I'd have to update A LOT of ActionMethods to do this - and most currently don't even use attribute routing.
thx.
Update
I've tried adding the below to RouteConfig.cs, but none of the Url's work for some reason:
routes.MapRoute(
name: "Default",
url: "keyword/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
string keyword = "keyword";
// keyword route - it will be used as a route of the first priority
routes.MapRoute(
name: "Defaultkeyword",
url: "{keyword}/{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
keyword = keyword
});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
also you need to add this Defaultkeyword route with few changes into your Area1AreaRegistration.cs file
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Area1_defaultKeyword",
"{Keyword}/Area1/{controller}/{action}/{id}",
new { Keyword = "Keyword", controller = "HomeArea1", action = "Index", id = UrlParameter.Optional }
);
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}

MVC homepage not working, RouteConfig and Global files look OK

I'm working in MVC 5 and have taken over a project. When I log onto the homepage "mydomain.com" it comes up with an error:
"Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. Requested URL:/"
If I type in mydomain.com/home/index it comes up with the home page as it should. I figure this is a RouteConfig.cs problem, but everything looks pretty default to me.
namespace Source
{
public class RouteConfig
{
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 }
);
routes.MapRoute(
name: "Glossary2",
url: "{controller}/{action}/{letter}",
defaults: new { controller = "Teacher", action = "Glossary2", letter = UrlParameter.Optional }
);
/* Will need to finish this later.
* When the contact us form is submitted, it should redirect to Home/Contact/{state}
* where {state} can be either 'Success' or 'Error', which will display
* an alert component in the view based on the provided {state}.
*/
routes.MapRoute(
name: "ContactSuccess",
url: "{controller}/{action}/{submissionStatus}",
defaults: new { controller = "Home", action = "Contact", submissionStatus = UrlParameter.Optional }
);
/* Will need to finish this later.
* When the contact us form is displayed, it should check to see if a reason for contacting
* us is already set. If it is, it should automatically select the appropriate reason on the
* dropdown menu.
*/
routes.MapRoute(
name: "ContactReason",
url: "{controller}/{action}/{reason}",
defaults: new { controller = "Home", action = "Contact", reason = UrlParameter.Optional }
);
}
}
}
I'm not sure what the CustomeViewEngine is doing and haven't really messed around with it yet. I've also inspected the Global.asax.cs file and it looks pretty standard as well.
namespace Source
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ViewEngines.Engines.Add(new CustomViewEngine());
}
}
public class CustomViewEngine : RazorViewEngine
{
public static readonly string[] CUSTOM_PARTIAL_VIEW_FORMATS = new[]
{ "~/Views/Selection/{0}.cshtml" };
public CustomViewEngine()
{
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(CUSTOM_PARTIAL_VIEW_FORMATS).ToArray();
}
}
}
Is there a way to trace down why the domain name is not getting routed to home/index? If I put the Default mapping at the bottom of the RouteConfig file it wants to automatically direct to the login page. Again, I'm not really understanding why this would be acting this way.
I think your route definitions are not in right order, the route order evaluates from top to bottom (the most specific path resolved first).
Hence, the default route should be take place as the last defined route:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// custom path at the top (or top-most depending on priority)
routes.MapRoute(
name: "Example",
url: "Example/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// default path at the bottom-most
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Additionally, all URLs defined in every route in the sample are in same pattern (using {controller}/{action}/{parameter}), hence it potentially conflict between each other. You can use plain strings to differentiate similar route patterns:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Glossary2",
url: "Teacher/{action}/{letter}",
defaults: new { controller = "Teacher", action = "Glossary2", letter = UrlParameter.Optional }
);
routes.MapRoute(
name: "ContactSuccess",
url: "{controller}/{action}/SubmissionStatus/{submissionStatus}",
defaults: new { controller = "Home", action = "Contact", submissionStatus = UrlParameter.Optional }
);
routes.MapRoute(
name: "ContactReason",
url: "{controller}/{action}/Reason/{reason}",
defaults: new { controller = "Home", action = "Contact", reason = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Note: Use RouteDebugger to find out which routes handled by RouteConfig when accessing specific routes.

Routing in ASP.NET MVC with multiple areas

I have two different areas in ASP.NET MVC Web Application.
1. Admin
2. Site
I want to have the default route of my application to the Site Area
i.e www.mydomain.com/Site/Home
For that I have configured the default route as follows.
But this configuration does not work for me.
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 },
namespaces: new string[] { "MyApp.Areas.Site.Controllers" }
);
}
The Site Area Route is as follows.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Site_default",
url: "Site/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
The Admin Area Route is as follows.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { controller="Login",action = "Login", id = UrlParameter.Optional}
);
}
Your help will be much appreciated
I think you've wrong controller namespaces.
Your main route definition should be:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyApp.Controllers" }
);
And your areas route like admin area registration should be:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Home" },
new[] { "MyApp.Areas.Admin.Controllers" }
);
Following steps to do:
Create a Project: Project Type: Framework 4.6.1 > WebApi and MVC Web
Project Layout will have [Areas Folder]
Inside [Areas folder] you'll find folder [HelpPage Folder]
Right-click >> [Areas folder] Select Area > "Give it a name: Administration"
Visual Studio Register everything for you.
Areas
Administration
Controllers
Models
Views
AdministrationAreaRegistration.cs
Filestructure example
Create a Controller > read write : Give it a name [Admin] and [View] Indexenter image description here
Then in your layout add link : Under the normal Home link
#Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
#Html.ActionLink("Admin", "Index", "Admin", new { area = "Administration" }, null)
Click on the link and see your Administration area

Custom Routing in mvc4

I am using mvc4 framework and .net framwork 4.5. I need a url like this:
www.examples.com/name (note: 'name' will be change dynamically)
which routes to the same page.
I have tried like this but getting error
My action method is like this:
public ActionResult Userlist(string status)
{
return View();
}
Route Config
routes.MapRoute(
"user",
"{status}",
new { controller = "Home", action = "Userlist" }
);
How can I create a route syntax and redirect this to a controller?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add this route config
routes.MapRoute(
name: "Default_Userlist",
url: "{status}",
defaults: new { controller = "Home", action = "Userlist" },
namespaces: new[] { "MyMvcProject.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcProject.Controllers" }
);
}
Don't forget to replace the "MyMvcProject" with the name of your project.
Issue
This is a bad practice because the value of the status could easily cause conflicts with your action methods.

how to rewrite mvc routes to aspx

I have home controller and Index action method. The below url works
http://localhost/home/index
Will it be possible to make it work like below
http://localhost/index.aspx
I am trying below code in Global.asax but does not works
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
Route Config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("MyPage", "create.aspx", "~/home/create");
routes.MapRoute(
name: "Customized",
url: "{action}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Reports",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Reports", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);
}
You should use MapRoute() instead of MapPageRoute(), as you are still referring to an MVC controller/action:
routes.MapRoute(
name: "Default2",
url: "index.aspx",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
PS: Remember to register the new route before other ones which may eventually interfere with it.
You can't MapPageRoute for non static content as you are doing. If you wan't to hide the Controller for your route the Customized already do this. If you are mixing MVC + WebForms you should fallow this guide to se how config your routes.
How to: Define Routes for Web Forms Applications

Resources