0

Is it possible to remove a folder from the URL so that domain.com/mvc/page/ becomes domain.com/page/ ?

This is an ASP.NET 4.5 MVC application running on Windows Server 2012 with IIS 7.5


EDIT:

/mvc/ is the folder containing the MVC web application, in the URL domain.com/mvc/page/whatever/else/

Organic
  • 247
  • 1
  • 21

2 Answers2

0

Ask i remember URL in MVC does not depend on folder structure but on Controller and Routing.(Attribute Routing)

 routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

. please check this link. ASP.NET MVC Routing Overview (C#). I think you can find more explanation there. For one link you can use Attribute

[Route("orders/{id:int}/{orderCode?}")]
    public async Task<ActionResult> Order(int id, string orderCode = "")
    {
   ////
    }
Tchaps
  • 865
  • 7
  • 21
0

Is it possible to remove a folder from the URL in ASP.NET MVC?

No.

MVC does not use files and folders to host its endpoints - it uses controllers and actions. Therefore, it is not possible to remove a "folder" from the URL, because there are no folders in the URL.

However, MVC uses .NET routing, which allows you to configure your URLs to look however you want. So, to make a route that matches /page as per your example, you need to insert a route to match that URL to your RouteConfig.cs file.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Route to domain.com/page that calls an action named Page on the HomeController
        routes.MapRoute(
            name: "PageRoute",
            url: "page",
            defaults: new { controller = "Home", action = "Page" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • I'm sorry I should have made my question clearer that /mvc/ is the folder containing the MVC application. Can RouteConfig.cs see and route the /mvc/ URL part? I assumed it could only route URLs inside /mvc/... if that makes sense. – Organic Jun 08 '16 at 09:15
  • The simplest solution is to remove the folder and host the application in the root of the website or [move the root directory of the website through IIS](https://forums.iis.net/t/1207046.aspx). But if circumstances dictate you must keep the subdirectory, you can use the [IIS URL Rewrite Module](http://www.iis.net/downloads/microsoft/url-rewrite) to [rewrite the root directory to the subdirectory](http://forums.asp.net/t/1479881.aspx?URL+Rewriting+the+root+folder+to+a+subfolder). This is not a problem you should try to solve inside of the application because it exists entirely outside. – NightOwl888 Jun 08 '16 at 09:45
  • The root of the website is a webforms website. As a proof of concept I have already tried to move the mvc application to the root of the website by combining the web.config, copying the mvc .dll to the root bin folder and copying the Views folder to the root. I got a 404 error when trying to load an mvc URL. – Organic Jun 08 '16 at 10:08
  • I have also already tried to rewrite the URL in IIS but can't seem to be able to rewrite an MVC application folder to the root of the website. I posted about it but no help yet: http://serverfault.com/questions/782482/remove-folder-from-url-of-mvc-application-within-webforms-website – Organic Jun 08 '16 at 10:11
  • To combine the sites into one project, you need to ensure all of the references are put in as well (such as System.Web.Mvc) and there needs to be an assembly created out of the source code for both sites. This of course implies that you need to have the source (project) of each site. See [this question](http://stackoverflow.com/questions/2203411/combine-asp-net-mvc-with-webforms). – NightOwl888 Jun 08 '16 at 11:20
  • Feels like I'm going around in circles because this is how it all started : ) By trying to combine the MVC into the current website. I've had no luck following online resources as most are quite old or talk about adding webforms to MVC or how create a new app using both tech. I have actually already seen the link you suggested, which is 5 years old. Things have changed a bit with VS2015 and newer MVC. I can't seem to find a solution that is specific to our setup trying to get 60 webforms projects to work nicely with a single MVC in the website root. We do have the source for both websites btw. – Organic Jun 08 '16 at 14:17
  • This has absolutely nothing at all to do with Visual Studio, other than the fact it doesn't support this type of scenario naively. You need to manually [ensure your references are all accounted for and correctly versioned](http://stackoverflow.com/a/35991676/181087) in your project file and all web.config files. It is painstaking, but it is possible. Just don't expect any help from Visual Studio or a step-by-step guide to lead you because there are so many possible states that an application could be in depending on how upgrades to it have progressed over time. – NightOwl888 Jun 08 '16 at 14:45
  • I suggest you go though one of the integration tutorials and setup a demo with the version of MVC and .NET framework you are targeting. Once you have it working, compare the assembly references with your current project and add/update them so they match the working project. You will also need to go through the `web.config` and `Views/web.config` and ensure all of the component versions are in sync with your assembly references. – NightOwl888 Jun 08 '16 at 14:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114144/discussion-between-organic-and-nightowl888). – Organic Jun 08 '16 at 15:50