Home MVC Web Application project under MVC Controller
Reply: 5

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#
Raphael Reply to 2018-01-16 08:22:36Z

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
  • ...

Create your admin Web Project inside of your primary Web project

Foo.Web

  • Administration
    • Properties
      • AssemblyInfo.cs
    • Foo.Admin.csproj
    • ...

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

Add an AreaRegistration implementation to your admin project

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName => "Admin";
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute
        (
            name: "AdminDefault",
            url: "admin/{controller}/{action}/{id}",
            defaults: new {controller = "Home", action = "Index", area = "admin", id = ""},
            namespaces: new[] {"Foo.Admin.Controllers"}
        );
    }
}

Modify Global.asax.cs to register areas

Add this to your project. Make sure it's called before your default routes.

AreaRegistration.RegisterAllAreas();

With this, all your controllers inside of Foo.Admin are found by ~/Admin/{controller}/{action} and all your controllers inside Foo.Web are found by ~/{controller}/{action}

cloudstrifebro
3#
cloudstrifebro Reply to 2018-01-19 03:39:28Z

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#
Nitesh Reply to 2018-01-19 11:56:37Z

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 }
        );
    }
}
Phil
5#
Phil Reply to 2018-01-19 23:34:53Z

Since you have 2 distinct MVC web applications I see 2 possible solutions...

a) create an Admin folder inside your main web application and set it as the root of your admin web application i.e. put your whole admin website into the admin folder. I expect this will require some tweaking of web.config of both apps so they can coexist.

b) leave each web application in it's own IIS virtual Application then add rewrite rules in the main web application for the /admin path to the true IIS path and you may need some rewrite rules in the admin app back to the main app if there is expected to be a navigation path to it.

ATL_DEV
6#
ATL_DEV Reply to 2018-01-23 20:24:04Z

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

<system.webServer>
<rewrite>
    <rules>
        <rule name="product.main-brand.com Redirect" stopProcessing="false">
            <match url="^\/?$" />
            <conditions>
                <add input="{HTTP_HOST}" pattern=".*product\.main-brand\.com.*" />
            </conditions>
            <action type="Redirect" redirectType="Found" url="http://www.main-brand-com/product/{R:0}" />
        </rule>
   </rules>
</rewrite> 
</system.webServer>

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

<system.webServer>
<rewrite>
    <rules>
        <rule name="admin.main-brand.com Redirect" stopProcessing="false">
            <match url="^\/?$" />
            <conditions>
                <add input="{HTTP_HOST}" pattern=".*admin\.product\.main-brand\.com.*" />
            </conditions>
            <action type="Redirect" redirectType="Found" url="http://www.main-brand-com/product/admin/{R:0}" />
        </rule>
   </rules>
</rewrite>

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/

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306043 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO