How to configure routes when the application is not at the root of the Asp.net mvc server?

Asked

Viewed 931 times

3

Locally my site has the following default route is everything works:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

            routes.MapRoute(
                name: "Home",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );


        }
    }
}

But my site is not in the root folder of the server, I have a virtual folder with the name website, getting like this:

http://www.teste.com.br/website

This way when I am going to make an access to a page the redirect keeps pointing to the root folder, and should come the name of the folder before:

http://www.sistemaguardiao.com.br/"folder is missing"/Recover password

Example:

@model Generico.Dominio.TB_USUARIO


@{
    ViewBag.Title = "Index";
}


@Html.Partial("_navbarPrincipal")
@Html.Partial("_PartialMensagens")


<div class="container droppedHover">


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
       @Html.ValidationSummary(true)

        <div class="row">
            <div class="span6">
                @Html.TextBoxFor(x => x.login, new { placeholder = "endereço de e-mail", @class = "form-control input-lg "})
                @Html.ValidationMessageFor(x => x.login)
                @Html.TextBoxFor(x => x.senha, new { placeholder = "password", @class = "form-control input-lg", type = "password" })
                @Html.ValidationMessageFor(x => x.senha)
            </div>
        </div>



        <br />

        <div class="row">
            <div class="span6">
                <button class="btn btn-lg btn-block btn-primary glyphicon glyphicon-log-in" type="submit" name="opcao" value="pesquisar" >   Login...</button>
                <a href="@Url.Action("Index", "RecuperaSenha")" class="btn btn-lg btn-block btn-warning ">
                    Recuperar Senha
                </a>
            </div>
        </div>


}


</div>

web confing:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>



  <connectionStrings>
    <add name="Conexao"
         connectionString="Server=PC-ESTUDO\SQLEXPRESS;Database=teste;User Id=teste;Password=123;"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>


  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>


  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
    <customErrors mode="Off"/>

  </system.web>



  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

thanks for the help

  • How you are doing to create the links in your Views?

  • @Ciganomorrisonmendez, I added in the question the login page, see the link that calls the Recoverpassword, in this case already comes without the folder "website"

  • What about hosting? How is it done? On IIS or a dedicated website service?

  • dedicated service, like in my example : http://www.teste.com.br/website

  • I can give you an answer, but you’d have to have access to web.config from the top directory.

  • @Ciganomorrisonmendez, Ok, you can access, web.config from the top directory would be what’s inside the Views folder or what’s outside?

  • The root directory.

  • There are 2 web config, one inside the project just above Global.asax and the other that is just below, which one needs?

  • The web.config just below.

  • added to the question

Show 5 more comments

1 answer

2


To make your application work in a subdirectory, change your Web.config to the following:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
<location path="." inheritInChildApplications="false">
  <connectionStrings>
    <add name="Conexao"
         connectionString="Server=PC-ESTUDO\SQLEXPRESS;Database=teste;User Id=teste;Password=123;"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>


  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>


  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
    <customErrors mode="Off"/>

  </system.web>
  </location>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

This statement:

<location path="." inheritInChildApplications="false">

Causes the settings at this point to be used only by the root directory application. Subdirectory specific settings are now considered.

Note that the section is not valid for the whole file. <runtime> does not enter the section.

  • the answer is this? <Location path="." inheritChildApplications="false">?

  • Yeah. I put the full example.

  • Yes, I saw, but only 1 line was the answer, This answer will not help many people, since the only difference this in 1 line that was not explained why of her, more all right, I thank your good will. I’m learning in the purebred, alone more I’m going.

  • I have improved the answer. I think it will now become clearer to more people what should be done. I apologize for the incompleteness.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.