How to host an ASP.NET MVC website as sub-directory - c#

I have an ASP.NET MVC website that I would like to place in a sub-directory of an existing web domain. This way the URL would be something along the lines of www.existingsite.com/newsite/login rather than www.newsite.com/login.
I've tried adding it to the RouteConfig.cs file like what's below but that didn't work at all.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "NewSite/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
After that I went into the properties of the project and set the virtual directory to be localhost:port/NewSite. That seemed to work until I attempt to decrypt a cookie with the user's information.
The decryption throws an error
Error occurred during a cryptographic operation.
It blows up on this specific line:
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
Is it possible to host an ASP.NET MVC website in a sub-directory or does it have to be at the root level?

Related

MVC app only rendering the _Layout.cshtml

I'm currently facing an issue with an MVC application I'm busy with, when testing the site in a local environment it shows the login page like it's supposed to as specified in the RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
}
But as soon as this gets uploaded the server it only renders the _layout.cshtml.
I have also attempted to specify the page in the application properties but this has not worked either.
Any advice or things to try would be greatly appreciated and thank you in advance.
Please note, when routing to the controller and views manually it all works fine, it's just the initial load of the site.

MVC configure route for mobile controllers

In my MVC 4 web appication if I access the Home page I call the following url:
sitename/Home
I now added a subfolder called Mobile to the Controllers folder.How can I configure routing to be able to call the Home controller in the Mobile folder like this
sitename/Mobile/Home
Here's my RegisterRoutes method:
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[] { "Test.Controllers" }
);
routes.MapRoute(
name: "Mobile",
url: "Mobile/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Test.Controllers.Mobile" }
);
}
ASP.NET MVC routes are order dependant and should go in order of most specific to most generic.
At the moment, if you type the url Mobile/Home/Index the routing will try and map that to:
controller: Mobile
action: Home
id: Index
using the default route and never get to your intended route map.
If you swap the MapRoute declarations around, then MVC will see that it starts with "Mobile" and use that route as intended.
#dav_i is 100% correct in that the route needs to be before the other one. The URL for mobile has Mobile in the URL, so you would have to have Mobile in any action links or redirects... So ideally, in your global.asax, you'd try to detect whether the browser is mobile, and redirect within there... But you still have to manage having Mobile in the URL. That's because functions like Url.Action and Html.ActionLink expect the controller and action to be the full URL, and this:
#Url.Action("Action", "Control")
produces:
/Control/Action
And as such, you'd have to handle this. Alternatively, you could use mobile views without having to worry about this. See this tutorial.

ASP.NET MVC Missing Default Action only for one controller

I don't know what happened to my website. From today the default action "Index" in only one controller doesn't work anymore.
If I call http://website.com/Valuation i get 403 error page because the webserver doesn't route my request and try to browse the folder. If I write http://website.com/Valuation/Index everything works.
I search in all the code but i can't find the problem, everything seems fine like other controllers.
How can i find the problem? Do you know if there are a known issue that cause that problem or you know if there is a trace\log\debugger of routing requests?
Thanks
Mic
Most probably the issue is you have a folder by name Valuation in your website root. Thats why the valuation index action is not working. Instead of routing to the Controller Action the url http://website.com/Valuation is routing to the Folder Valuation.
Delete this folder Valuation from your root or rename it then this url http://website.com/Valuation will work.
Also check if the ValuationController has the public ActionResult Index() ([HttpGet] method.
Check ~\App_Start\RouteConfig.cs file
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}
);
}
}

My mvc 2 application serves 404 for all pages except home page

I have an mvc 2 application that is running in ii6 in the test environment and production server.
The test enviroment runs just fine, but after moving to production all pages except the home page server up 404 errors.
I have followed step 2, here: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/ and added a .aspx extension to the route, and have tried the wildcard mapping. It doesn't appear to make any difference.
I put the diagnostic file found here: http://bradwilson.typepad.com/blog/2010/03/diagnosing-aspnet-mvc-problems.html into the directory and loaded it, but it does not report any errors or problems.
I even wiped the test server and reinstalled the app from scractch, setup the wildcard mapping and it worked fine.
Right now the pages are being routed like: Home.aspx/About and my routing table looks like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"NewEmployee",
"{controller}.aspx/{action}",
new { controller = "NewEmployee", action = "Index" }
);
routes.MapRoute(
"Admin",
"{controller}.aspx/{action}",
new { controller = "Admin", action = "Index" });
routes.MapRoute(
"AccessMaster",
"{controller}.aspx/{action}/{id}/{subid}",
new { controller = "AccessMaster", action = "Index", id=UrlParameter.Optional,subid=UrlParameter.Optional });
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}
I'm at a loss here. Am I doing something wrong? Is there something wrong with the server?
Try registering ASP.NET with IIS using the following command:
aspnet_regiis /i
Also make sure that you have enabled the correct version of ASP.NET in IIS (Web Service Extensions folder):
You also have many unnecessary routes routes. For example the NewEmployee and Admin routes are totally equivalent meaning that only the first route in this list will ever be matched. But that's another problem, it is unrelated to the deployment errors you are getting. You could fix your routes once you make your application successfully run.
It looks like deleting and recreating the virtual directory magically fixed whatever was wrong with it.

MVC Site - Ensuring the default entry view is always correct

I have a MVC site with AD authorization. This is all working fine. I publish the site to the webserver and call the site directly (http://intranet). If I have not logged in for a while (I have an authorised cookie with a 30 minute TTL), I am prompted to log-in and if successful I am redirected to the homeController's index view. This is great and as expected.
If I keep the session open (browser open) and browse away from the site, if I then browse back to http://intranet, I am not challenged as I have recently authenticated but the default page is from a different controller and not the home page view.
How can I stop this from happening? It cannot be a session setting as this is not a new session and the routes appear correct - they are not beng called at this point anyhow.
Please MVC guru's advise....!
Register route block is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
// standard MVC route regsitration
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"PaginatedTimesheets", // Route name
"{controller}/{action}/{page}/{view}", // URL with parameters
new { controller = "Timesheets", action = "Index", page=0, view=0 } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
What is your default route look like? It should be going to your default controller/action specified in your routes.
Fixed with redirect

Categories