Home MVC Web Application project under MVC Controller

# MVC Web Application project under MVC Controller

Patrick
1#
Patrick Published in 2018-01-13 01:43:57Z
 I have 2 distincts MVC Web Applications accessed from: 1) product.main-brand.com (solution landing page) 2) admin.product.main-brand.com (solution admin landing page)  The product is going to change the location from: product.main-brand.com to www.main-brand-com/product  And the admin has to change from: admin.product.main-brand.com to www.main-brand-com/product/admin  I can't create a Virtual Directory for the admin because www.main-brand-com/product is a controller. For example, NopCommerce does that, we can go from www.shop.com to www.shop.com/admin and it's changing project and not controller/action. How does he do that?
Raphael
2#

Here are some steps to achieve a similar solution like in nopCommerce 3.9

## Create your primary Web Project

Foo.Web

• Properties
• AssemblyInfo.cs
• Foo.Web.csproj
• Global.asax
• Global.asax.cs
• ...

Foo.Web

• Properties
• AssemblyInfo.cs
• ...

Administration folder must be created using "Add project wizard" or using explorer. Do not create that folder using solution explorer.

## Remove Global.asax from admin project

You don't need this

public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName => "Admin";
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute
(
defaults: new {controller = "Home", action = "Index", area = "admin", id = ""},
);
}
}


## Modify Global.asax.cs to register areas

AreaRegistration.RegisterAllAreas();


cloudstrifebro
3#
 I would recommend that you do look into attribute routing. https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/ This allows you to do this: [Route("/product/admin")] That attribute decorator goes on your controller method after you setup attribute routing. Hope that helps.
Nitesh
4#
 This can easily be achieved in asp.net mvc application by using the feature called Area (for detail see https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas). Your main application will work for www.main-brand-com. You can add an Area for Product to be referred by www.main-brand-com/product and another Area called Admin to be referred by www.main-brand-com/admin. You can add an Area in asp.net mvc application by right clicking on web application project then selecting Add from the quick menu and then select Area. It will ask for name of Area to be created. After giving name and clicking Ok it will create an Area with that name in Areas folder in your web application project. The newly created Area will have same folder structure like main application like Controller, Model, View etc. folders. It will also create a class (AreaRegistration.cs) for registering the routing details for this Area. The content of this class may look like below code. public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 
 As a rule, if you're working with an externally maintained platform, you should aim to make as few code changes as possible. You will save yourself the work of merging, testing and deploying your code changes when there's an update. This is especially important if the update is a critical one. In this particular case, it is best to let IIS to reroute your URLs. It has no code impact and offers a major benefit. Visitors and search using deprecated urls are automatically redirected to newer ones. This is especially important for search engines. If they encounter a 404 error, they will often remove the urls and even the entire site. In other words, Você pode perder muitas vendas! You can reroute your urls in the IIS Manager using the Url Rewrite Module or through web.config files. I recommend web.config files since it can be uploaded and kept together with your project. For your existing product subdomain site, add the following to the system.webServer section of the site's web.config file: product.main-brand.com to www.main-brand-com/product   For your existing admin subdomain site, add the following to the system.webServer section of the site's web.config file: admin.product.main-brand.com to www.main-brand-com/product/admin   These addition will redirect users to the new URLs. Refer to the following article for how to setup IIS url rewrite for further information: https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-global-and-distributed-rewrite-rules Changing the redirection in IIS is one part, but your application needs to understand how to route the new url scheme internally. Without your app's specific routing code, I can't be of much help. If your application, however, is using subdomain's url parameter and you want to change it, refer to the following article: https://benjii.me/2015/02/subdomain-routing-in-asp-net-mvc/