URL Routing Not Working (asp.net 4.5, webforms) - url

I've been trying to figure out how to get URL routing to work on my box all evening. I'm pretty sure I'm doing things according to the online tutorials (http://msdn.microsoft.com/en-us/library/vstudio/dd329551%28v=vs.100%29.aspx) but for some reason it's not working.
This is what I currently type in the address bar: /MA1/DoSurvey.aspx?id=42
But I want the URL to work like this: /MA1/DoSurvey/42
According to the tutorial I have to modify:
1: Global.asax.cs by adding
RouteConfig.RegisterRoutes(RouteTable.Routes);
into the Application_Start method
2: RouteConfig.cs by adding
routes.MapPageRoute("","DoSurvey/{id}","~/MA1/DoSurvey.aspx");
into the RegisterRoutes method
3: Add
using System.Web.Routing;
as a reference in the DoSurvey.aspx page and then
4: Grab the querystring variable by using
string SurveyIdQueryStringValue = Page.RouteData.Values["id"] as string;
in the DoSurvey.aspx page
what am I missing?

I Add this code to web.config and everything worked fine, try it
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRoutingModule" />
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</modules>
<handlers>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

Related

Extend Session Timeout

I need to extend the session timeout in my .net mvc application.
I am doing a .get to an action result. It seems to work properly.
Just wanted to confirm that a GET or POST would do the same thing in terms of extending the session timeout.
<sessionState customProvider="DefaultSessionProvider" mode="InProc" timeout="1500">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
give this in web.config file in your solution..timeout can be changed as per your requirement.

How can I get ASP.NET MVC 4 to accept routes that look like filenames?

I'm working on a custom routing extension for ASP.NET MVC4 that uses reflection to generate routes based on attributes applied to controllers. It's a fork of Brandon Byars' RestMvc project and I'm updating it for .NET 4.5.
The attraction is that it allows one to write controllers that describe their own routes, like so:
public class EchoController : RestfulController
{
[Get("echo/{thingToEcho}.{format}", "echo/{thingToEcho}")]
public ActionResult Echo(string thingToEcho, string format)
{
if (format == "xml")
return new ContentResult
{
Content = string.Format("<echo>{0}</echo>", thingToEcho),
ContentType = MediaType.Xml
};
return new ContentResult
{
Content = thingToEcho,
ContentType = MediaType.PlainText
};
}
public override ActionResult Options(string resourceUri)
{
SetAllowHeader(resourceUri);
return new ContentResult {Content = "Options body goes here..."};
}
}
I'm running into a problem with routes in the format
[Get("echo/{thingToEcho}.{format}", "echo/{thingToEcho}")]
The idea here is that the client can make a request such as GET /echo/test.xml and the controller can then return a response in xml format instead of plain text, as demonstrated in the EchoController above.
In my fork of th eproject, all the routes are being correctly generated, but the "echo/{thingToEcho}.{format}" route doesn't work and I just get a 404 page. The same request but without the ".xml" works correctly and echoes back the expected text, so it seems like the route handler is doing its job, at least partly.
I'm a bit inexperienced with these technologies but I think this may be bound up with the handlers that are registered in the web.config file. In Brandon's original code targetting .NET 3.5 and MVC3, he has the following handlers:
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="MvcHttpHandler"/>
<remove name="UrlRoutingHandler"/>
<add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</handlers>
In my project, I wanted to get this working on top of the MVC4 basic Web App template, so I created a new MVC4 project and dropped Brandon's controller onto it. In the MVC4 web.config, the following handlers are registered:
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Again, I haven't added these handlers, they are the defaults generated by the MVC4 project template.
Could this be the source of my problem? How would I solve this?

Add MVC to an existing ASP.net website

I've searched the archives and also googled about it but it seems I can't find a guide / best practices / how-to on how to smoothly add features to an existing ASP.net 4 website, adding them in the latest MVC.
I have a website that was built with ASP.net 1.0 and progressively has been upgraded to 4.0, now I want to switch to MVC but re-create all the website in MVC is a too long process since the site has a lot of features, and some parts are just good in webforms.
The best way for me should be to be able to smoothly add MVC to this existing ASP.net 4.0 website and progressively migrate the existing webforms to MVC.
(Don't want to start a webforms-MVC flame, just looking on some tips on how to avoid common mistakes.)
add System.Web.Mvc reference in ASP.net website
for support MapRoute,you need add the following code in web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
3. add assembly reference in web.config:
<compilation defaultLanguage="c#" debug="false" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
4. add routes map in Global.asax,such as:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}

MVCContrib Portable Area No Intellisense for ViewPage<T>

I am trying out portable areas using MVCContrib.
In general these work well and it seems to be a good way to share controllers\views between web projects.
The only problem that I'm having is that Intellisense (specifically, for the HtmlHelper) is not working in the view for strongly typed views i.e. ViewPage.
The intellisense does work however when the view is a plain 'ol System.Web.Mvc.ViewPage
A similar questions has been asked here:
MvcContrib Portable Areas View Intellisense?
But these suggestions don't seem to make any difference.
I am using MVC 2, the portable areas are in their own class library as in the MVCContrib sample code.
I'd also like to add that the MVC sample code gives me the same behaviour, if I change the sample project to make the view page strongly typed then intellisense stops working.
Are other people having the same problem ?
Does anyone know the cause and or solution ?
the web.config from my Views folder is as follows:
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*"
type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
</handlers>
</system.webServer>
</configuration>
This was a problem with ReSharper intellisense in VS2010, ReSharper v5.0.
If I change my ReSharper options (ReSharper->Options->Intellisense->General) to use Visual Studio intellisense then it works!!

ASP.NET MVC Strongly Typed Partial View, gives could not load type error

I am attempting to create a strongly typed view with a "MVC View User Control" that is being rendered using Html.RenderPartial(). The top of my ascx file looks like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.Collections.IEnumerable<string>>" %>
There is nothing else on this page, currently.
When I execute the app and load the page that renders this control, I get the following error:
Could not load type 'System.Web.Mvc.ViewUserControl<System.Collections.IEnumerable<string>>'.
So, then I simplified it:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>
And then, just in case it needed to be fully qualified:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.String>" %>
Everytime I get the same error (substituting type). what am I doing wrong here? I'm on .NET 3.5 with ASP.NET MVC 1.0 RTM.
I got it working. I followed the instructions from http://www.codewrecks.com/blog/index.php/2009/04/05/could-not-load-type-systemwebmvcviewpage/ and that did the trick for me. I should note that I also upgraded to the ASP.NET MVC 2.0 RC as of 3/17/2010 first. The problem persisted for me still until I followed the instructions on that page. I'm not sure if a fresh MVC project does this for you now or not.
The solution, in case the referenced page goes away, was to add a Web.config to my Views directory, and put this in it:
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*"
type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
</handlers>
</system.webServer>
</configuration>
I should also note that for MVC 2.0 you need to update the version #'s in the config.

Resources