Want to add MVC4 to WebForms app, don't have access to global.asax file - asp.net

I did some research on "mixing and matching" with mvc4 and webforms and came across a few links, notably This one.
Seems doable, except that I'd like to add this to an existing webforms app whose global.asax codebehind I don't have access to. I update my part of the current webforms app by dropping my dlls in the bin folder of the application root.
Is there another way for me to register the routes, filters and bundles if I can't do it in the application_start method in the global.asax?
In other words, where would this code go?
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}

You would have to hack it in in some ugly way or another so the best solution would be to persuade the guys controlling global.asax to give you some kind of hook.
But the routes could ve registered anywhere as long as it only happens once and early in the application lifetime, so perhaps a static constructor somewhere?
The problem would them be to ensure that the type holding the ctor is accessed during app start which could introduce a new headache but it could be doable.
But again: some kind of hook in the startup event would be preferable.

Take a look at the App_Start folder. According to the article on code project:
"And also we can avoid touching Global.asax file when using any new
nugget packages that needs to use Application_Start from Global.asax"

Related

Routing for Single Page Application in ASP.NET Core

I have a Single Page Application written in JavaScript, and I use HTML5 history API to handle URLs on the client side. This means any URLs sent to the server should cause the server to render the same page.
In the ASP.NET MVC 5 I wrote this code to do this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// ...
routes.Add(new Route("{*path}", new MyRouteHandler()));
}
}
public class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return WebPageHttpHandler.CreateFromVirtualPath("~/index.cshtml");
}
}
This worked really well. No matter what URL the server gets, it renders index.cshtml. Note that I am able to use a .cshtml file (as opposed to an .html file) which means I can have some C# code to dynamically change what .js scripts are included, append version numbers to .css files, and so on. What's more, I didn't have to implement controllers and views and so on just to render this .cshtml file.
Now we come to the question: How do you do this in ASP.NET Core? I have been reading the documentation, but I don't see how to render a .cshtml file without adding controller classes, view folders and other rigmarole.
Anyone know the equivalent code in ASP.NET Core?
Currently to run a CSHTML page "the normal way" in ASP.NET Core requires using ASP.NET Core MVC.
However, there is a planned feature that is being worked on that is somewhat similar to ASP.NET (non-Core) Web Pages, where you can have standalone CSHTML files. That feature is being tracked here: https://github.com/aspnet/Mvc/issues/494 (and as far as naming for the new feature, that is being tracked here: https://github.com/aspnet/Mvc/issues/5208).
There's also a sample of how to render an MVC view to a string (e.g. to generate an email, report, etc.), and that sample is available here: https://github.com/aspnet/Entropy/tree/dev/samples/Mvc.RenderViewToString
But to use this sample in the scenario you describe, you'd have to do some extra plumbing to wire it up as its own middleware (not a lot of plumbing; just a little!).
It's also worth noting that in your scenario you probably don't want all URLs going to this one view, because you still need the static files middleware running first to handle the CSS, JS, images, and other static content. Presumably you just want all other URLs to go to this dynamic view.

How do I use Global.asax files in ASP.NET applications?

How do I use a Global.asax file in my ASP.NET code?
Is there some kind of include statement, sort of like <script type="text/javascript" src='xxx.js'></script>?
You mean global.asax right? Just add it to your project. The code you put in there will be picked up by the ASP runtime and executed as expected.
It's a file. It goes in your application folder (the same folder your asp.net files go in.)
You'll use Global.asax for asp.NET, and Global.asa for classic ASP.
Right-click your project folder, add new item, pick global.asax from the options, and you're there.
(Which is to say, you don't have to make a reference to it in your .aspx pages like you do for javascript or css, etc. The events that global.asax handles are global (hence the name...), not page-specific.)

ASP.NET MVC - How to make it work with IIS6

I am having some issues with deploying my MVC 2 application on a IIS 6 server.
I have the following project structure:
/
App/
Controllers/
Helpers/
Infrastructure/
Models/
Views/
Public/ # This folder contains CSS and JS files
Global.asax
Web.config
I have a custom System.Web.Mvc.WebFormViewEngine that tells my application to lookup the views in /App/Views instead of the default /Views.
It works fine on Cassini and IIS 7.5.
I need to deploy my application in a virtual directory on IIS 6 and I am getting 404 errors when trying to access any of my controllers.
I read that I needed to add a Default.aspx with the following code behind:
protected void Page_Load( object sender, EventArgs e ) {
HttpContext.Current.RewritePath( Request.ApplicationPath, false );
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest( HttpContext.Current );
}
It actually called my default controller, and showed the corresponding view, but it's the only page I've been able to get so far.
I tried to enable the wildcard mapping, it didn't change anything. But I'm using ASP.NET 4.0, and it enables routing of extension-less URLs.
I'm not really sure what to do now, I'm not finding any other helpful sources of information on the Internet.
How could I make it work?
See this walkthrough by Phil Haack.
Can't comment yet, but that walkthrough is it.
I did wildcard myself.
It was a while ago, so I don't remember the damn details of what I had to do to get it fixed now, but it took me a few hours.
I was missing some really small detail in his instructions, if I remember correctly. What error/incorrect behavior are you getting? You might trigger my memory.

Reasons why WebResources cannot be found

Hi I am having trouble getting an embedded js file to work.
I have tried all of the following:
Running Cassini development server (VS2008, .NET 3.5)
Added [assembly: WebResource("MyNamespace.MyScriptFile.js", "text/javascript")] above the class's namespace declaration.
Script file has build action "Embedded Resource".
Tried registering during OnInit, OnLoad and OnPreRender
Script file is in the same assembly and namespace as the control registering it.
Opened assembly with Reflector and verified the name of the resource is correct.
Does not work using any of the following methods:
ScriptManager.RegisterClientScriptResource(Page, GetType(), "MyNamespace.MyScriptFile.js");
Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.MyScriptFile.js");
Page.ClientScript.RegisterClientScriptInclude(GetType(), "key",
Page.ClientScript.GetWebResourceUrl(GetType(), "MyNamespace.MyScriptFile.js"));
Other WebResource.axd files are being found - only this one is not being found.
The request for the resource returns a 404 page with an exception listed: "*[HttpException]: This is an invalid webresource request.*"
Using the ScriptManager.RegisterClientScriptResource produces the exception:
"*Web resource 'MyNamespace.MyScriptFile.js' was not found.*"
In your example code, you are making a call to GetType()... the type is used as the starting point for the search. Depending on where you are making your call to GetType(), you may not be getting back what you expect. Since ASP.NET dynamically compiles types for your pages and custom controls, GetType() may be returning a type defined in a new assembly built by ASP.NET.
You could try doing typeof(SomeType) instead, where SomeType is appropriate based on the location of your resource (ex. the control type you're working with).
maybe your resource file is located inside folder(s) in project?
if so, you should specify another name/location string in assembly and when you register the script
Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.Folder1.Folder2.MyScriptFile.js");
[assembly: WebResource("MyNamespace.Folder1.Folder2.MyScriptFile.js", "text/javascript")]
usually that's a common problem
Try implementing your own ScriptManger and then adding the embedded file from there. Here's an example
public class MyScriptManager : System.Web.UI.ScriptManager
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
RegisterClientScriptResource(this, typeof(ScriptManagerExtension), "MyNamespace.MyScriptFile.js");
}
}
An alternate cause of this issue - you've been too heavy handed with your Global.asax, and you've said that everything else after your rules gives you the homepage. Not so clever of me, an hour or two wasted on that!

How to add a reference to my Web Service Proxy from a Custom Class

I'm creating a custom class to abstract out some of the repeated SOAP header work. I want to reference a Web Service Reference I just created in my custom class so I can create an instance of it. How do I reference it?
Notice I said I am trying to reference a Web Service "reference" (right click in VS and I added a "Web Service Reference" not a "Web Service"). So I'm trying to create an instance of that Proxy class that was created in MyCustomClass.cs
Once you have the reference created you need to add an import(vb) or using(c#) statement in the code file you want to use it. After that you simply need to instantiate an instance of the web service class.
// add the service reference
using ServiceReference1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//create the instance of the web sevice of the class
SomeWebService sweb = new SomeWebService();
//call the web services HelloWorld Method
sweb.HelloWorld();
}
}
Hopefully this was what you were asking for
This is more Web Site weirdness. I recommend that web sites be used only for pages, images, css, js, etc, Anything else should be done in a separate project, and the web site can reference the other project.
I avoid web sites like the plague, so I've never had to make this work, but consider that web sites don't build. Instead, various things are built on the fly, when the site is used. There will be no Reference.cs file in a web site.
In more recent versions of Visual Studio the using statement for these web service references also has had to include the project name.
Using cptScarlet's original code example, change the first line to look like this:
// add the service reference
using MyProject.ServiceReference1;
When you type in your project name, the class and/or namespace of the objects created in the web reference should show up in the intellisense.

Resources