Session is NULL when running ASP.NET MVC inside of ASP.NET - asp.net

If I create an ASP.NET Web Application project and then add an ASP.NET MVC 2 to it using the default routes defined like so
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { action = "Index", id = UrlParameter.Optional }
);
The Session object is NULL when I try and access it in the action methods of the controllers. If I change my routes to this.
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { action = "Index", id = UrlParameter.Optional }
);
Everything works just fine. For whatever reason having the .aspx extension allows for session to be used, but the later doesn't. I'm using .NET 3.5 for everything.
Any ideas??? Thanks!

The solution is to add runAllManagedModulesForAllRequests="true" to the configuration>system.webServer>modules tag in the web.config.

Related

Url Rewrite: Should www re-writing be in the app or the web server?

I have to redirect non-www urls to use www. I have a choice of doing this in IIS 7 or code this logic in my ASP.Net application.
In terms of portability I would've thought writing this in the application itself might be better.
Is there a preferred method for achieving this or is it just preference?
When you do this in IIS7, all it does is modify your Web.Config files for you. I would have said that that was the conventional way of doing things.
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
I think, it is more rational to do this in the application. For example we have it defined like that:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"User", // Route name
"u/{user}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Hence we have plenty of flexibility and can have specific tokens in our links, etc. Yet if there was no need for that - we would use IIS base. So, I guess - whatever tickles your pickle here :)

MVC route conflicts with service route

I'm trying to convert the existing web project to MVC. I made a standard MVC project in VS 2012. It added routing configuration. My existing project already contained routing entry used by WCF services. So now routes are configured like this:
// Was here before, used by services
routes.Add(new ServiceRoute("AppServer", new WebServiceHostFactory(), typeof(MyService)));
// the rest is added by vs to configure a default controller
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I realized that service part should always go first because it is more restrictive. Service doesn't work if it is put to the end. But now the problem is that
#Html.ActionLink("text", "MyAction", "MyController")
now generates me the links of type
http://localhost/AppServer?action=MyAction&controller=My
instead of
http://localhost/My/MyAction
what I would expect.
Does anyone know how to make service route and mvc-related routes "live in peace" i.e. not to affect each other?
Try and put the MapRoute on top but add a fourth parameter for contraints, like this:
new { controller = "regex-for-!=-AppServer" }
This way, when creating a link the helper will use the first route found, but still incoming requests to "/AppServer" will be skipped and processed by the ServiceRoute.

rename controllers asp.net mvc

I have a bigger project with about 9 controllers. Now at the end of the project the url requeirement change. How to deal best with this situation - renaming the controllers seems just too cumbersome ... I need to change all links in servercode and javascript
Your problem can be solved by changing your existing routes. In your global.asax you'll find a code fragment like this
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
That maps an URL '/Controller/Action/Id' to Controller, Action and Id. You can provide routes like this
routes.MapRoute(
"RefactoredRoute", // Route name
"SomeChangedURLBase/{action}/{id}", // URL with parameters
new { controller = "Controller", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
to route requests to /SomeChangedURLBase... to be handled by Controller.
Be aware that these routes should be registered before the default route to avoid that links generated in views point to the default route and generate the old URL.
you could change the routings in global.asax
just change the method RegisterRoutes
here you can find some more informations.
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
cheers

System.Web.HttpException: The file '/StudentPortal3G/Home.mvc.aspx' does not exist

Getting this error, everytime my home/INdex loads in my MVC3 app on Server 2008.
System.Web.HttpException: The file '/StudentPortal3G/Home.mvc.aspx' does not exist.
Tried all of this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Default2", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults );
);
routes.MapRoute(
"Default3", // Route name
"{controller}.mvc.aspx/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults );
);
Views\Home\Index.aspx exists.
IIS7 is not supposed to need the .* handler.
Do I need to set the aspx handler to not check existance of file? But the file exists?
If this is the answer, how do I set it on iis7, I wasn't able to find that to try it.
Isn't there a way to do it in the handlers section of the web.config?
Again I found a few hint's but I'm not quite getting it.
Thanks,
Cal-
Did you add appropriate Views? In this case, you should have Views\StudentPortal3G\ (assuming that StudentPortal3G is a Controller) directory with Home.aspx inside and it maps to /StudentPortal3G/Home
Why are you trying to load Home.mvc.aspx at all is beyond me. I'd suggest you fire up RouteDebug to see which rules you are missing...

What's the proper configuration or routing so that Default.aspx, "", and "~/" don't get routed?

I've got glimpse.
it's highlighting an empty line on the routes tab
Match Area Url Data constraints DataTokens
True Root -- --
Locally it seems cassini doesn't properly emulate a virtual directory so changing from localhost to localhost/site doesn't seem to grant any additional testing insight.
Local testing
cassini (windows 7 (32 and 64 bit are available))
doesn't seem to use or allow integrated
deploy environments
IIS7 Integrated
Tried a http routing setting to push / to /site however I ran into trailing slash issues (/site would route to /site/site, while /site/ would work as expected
web.config
tried a few ways of setting runAllManagedModules=false with various errors
using <remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /> that was linked
tried
I need /site (/~) requests to go to ~/Default.aspx
would love for a way to have / go there as well.
How do I do it?
To enable Default.aspx as the default url you just need to remove your route handling so that there's no catch-all route.
By default all MVC 3 projects have:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This is a catch-all route that will match everything you pass in your url. If you remove this and no path is defined then it will use whatever you set up as your default page in your web.config.
To map /site to Default.aspx you just need to add a special route:
routes.MapPageRoute("SitetoDefault", "site", "~/Default.aspx");
This will see the /site and route it to your default page.
You'll have to be careful when adding any other routes to your routing table. All of them will have to have a controller defined or you'll have to add a routing constraint. Something like this:
//controller defined
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
//route constraint
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}, // Parameter defaults
new { controller = "^(products)|(account)|(home)$" }
);
Here is more detail on creating more complex routing: http://www.asp.net/mvc/tutorials/creating-a-custom-route-constraint-cs

Resources