Running a part of MVC application separately under webforms application - asp.net

I have a working MVC3 application with different modules. I want to use one of those modules as a separate application under IIS, this separate application will be under an already running WebForms application.
e.g.
My MVC application is running at http://mymvcappdomain.com/
There is a feature under this called "MyFeature" which runs at http://mymvcappdomain.com/MyFeature
I want another site (WebForms application) http://mywebformssitedomain/ to display exactly as http://mymvcappdomain.com/MyFeature when I browse to http://mywebformssitedomain/MyFeature
Is it possible? If so, how?
What I have already tried is dynamically registering selected number of routes based on the URL. I thought it'd work but as per the stackoverflow topic (http://stackoverflow.com/questions/2518057/request-is-not-available-in-this-context), I am not allowed to access the request object in the global.asax for registering routes.
Many thanks in advance!

Actually I was able to achieve what I wanted by changing the home controller. But it was purely because my requirements were not too complicated. The part of the MVC app I wanted to run was kind of standalone without any interference from other modules. But I guess the approach should work in resolving the issue.
public ActionResult Index()
{
if (URL is different to your normal MVC App URL)
{
return RedirectToRoute("YourCustomRouteForModule");
}
return View();
}

Related

ASP.NET/ServiceStack Root URL at startup

I'm trying to setup a ServiceStack template loosely based on the existing ASP.NET with razor template. The services to be created using this will be hosted in a variety of locations. What I would like is for them to be able to register themselves with a central server.
What I was hoping to do was to add some code to Application_start (or apphost) which would perform the registration however I can't find any way of getting the root url of the application. The normal method of using the request object doesn't work as there isn't a request object at that point.
If I can't get this from asp.net I'm wondering if there is a servicestack call I can make which can give me what I need
The URL for where an ASP.NET Web Application is hosted at is only available at runtime, inferred from the incoming are Request URL, so you won't be able retrieve it at Startup.

How to Create a URL to an mvc Action in a Different WebForm Project in the Same Solution?

I have two projects one asp.net mvc and one webform in the same solution. You would know if you can use both the web projects. Specifically, it would be able to use some of the action mvc project in the webform project.
Routing is project-specific. Regardless of whether your projects share the same solution, there's no easy way to generate a URL from one project in another. This is mostly due to the fact that other factors play into what URLs are generated by project other than just the routes it defines, such as virtual directories, domains, etc. These things are only known by the specific project while it is running.
As a result, the only way to truly get the URL for a route from a project is to get it from that project, while it is running. That means, you need to set up some sort of endpoint that you can send an HTTP request to, which would return an appropriate URL. Then, in your other project, you would have to use something like HttpClient to issue a request to that endpoint with whatever information it would need to generate the URL.

Working with both MVC and a WebApi-projects

We have this web.api-project currently residing in api.mydomain.com. It is as a backend for a mobile app and also a superadminlike javascript application. We also have this new public facing website (using Umbraco CMS, in the same VS-solution) wich currently holds just your plain information (with pages like "about us", "get the app" etc) on www.mydomain.com
We've setup a asp.net membership across theese domains and are now looking to find a way to have the public site host pages like login, edit profile, start subscription etc. The controller(s) for theese views exist in the web api-project. Views would presumably exist in the MVC-project.
I am not sure what approach to take. I would preferably be able to use the viewmodels that already exists in the solution and leverage the whole Modelstate/validation-thing, returning View() from the WebApi-project etc.
We could make our edit profile-forms just to POST to the api-project (CORS is a no-go), but then the api project would have to use the hackish Request.CreateResponse in order to redirect the user to some hardcoded place back on the public site, right?
Thanks.
When I've used web-api to talk back and forth to a front-end umbraco site I've tried to make it as dumb as possible and that seems to work well. It feels wrong to couple up you web-api project to do more than send data when requested, using web-api controllers to affect the UI/Views of a website sounds wrong.
Have you considered using Umbraco Members & then configuring them to use asp.net membership (http://our.umbraco.org/wiki/how-tos/membership-providers/how-to-integrate-aspnet-membership-control-with-umbraco) - in that way the Umbraco front-end can deliver tailored stuff to asp.net membership authenticated users without all the to-ing & fro-ing from your web-api?

Use System.Web.Mvc in non MVC ASP.NET Web Forms Application

We are developing an ASP.NET Web Forms application with REST modelled URL, for which we are using Route tables in global.asax. However, recently we came over a problem where the WebResource.axd and ScriptResource.axd files were getting routed as well. We wanted to use the RouteCollection.IgnoreRoute function. However, since it in System.Web.Mvc, it would mean that we would need to reference that dll in a non MVC Web Forms application.
Is it safe for us to continue with the approach. If anyone has a better way to ignore routes from within the Web Forms application please do share. Please also note that the application has been extensively developed in Web Forms and moving to MVC at the current stage is not feasible.
For web forms you need to use System.Web.Routing
Put this is Global.asax
void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapPageRoute(
"Home", // Route name
"WWWWWW", // Route URL
"~/Default.aspx" // Web page to handle route
);
}
Did you know you could safely run an ASP.NET Web Forms and MVC application combined? Here are some links:
http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx
http://www.packtpub.com/article/mixing-aspnet-webforms-and-aspnet-mvc
Also, you can just use URL routing in ASP.NET 4 applications, as Registered User pointed out above. Read more about it here: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

nhibernate session created on every web request in asp.net MVC2

I have an ASP.net MVC2 application that is using NHibernate for data access. On every request, even static file requests (images, javascript), a new session is getting created. So for a single view where I'm returning a list, I'm creating around 15 session that don't load anything.
is there way to only create sessions when they are required?
I'm currently using Castle.Windsor to inject the session into my Controllers.
Is there a way to filter out static file requests?
It sounds like you need to exclude those paths in your routing:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// add these lines
routes.IgnoreRoute("{resource}.jpg");
routes.IgnoreRoute("{resource}.js");
}
Although if you ask me, a request for a static file shouldn't be instantiating a controller. You might want to take a look at your code and figure out why it's doing that.
Also I figured it out that this isn't a configuration with NHibernate or MVC. It is that ASP.net development server services every request through ASP.net. IIS will not send static file requests through ASP.net unless configured.
From Here (http://www.asp.net/hosting/tutorials/core-differences-between-iis-and-the-asp-net-development-server-cs)
Another core difference between IIS
and the ASP.NET Development Server is
how they handle requests for static
content. Every request that comes into
the ASP.NET Development Server,
whether for an ASP.NET page, an image,
or a JavaScript file, is processed by
the ASP.NET runtime. By default, IIS
only invokes the ASP.NET runtime when
a request comes in for an ASP.NET
resource, such as an ASP.NET web page,
a Web Service, and so forth. Requests
for static content - images, CSS
files, JavaScript files, PDF files,
ZIP files, and the like - are
retrieved by IIS without the
involvement of the ASP.NET runtime.
how are you creating the session, as the .jpg should not ask for an instance of ISession, therefor castle will not create one (from my understanding), could you post the castle setup, and how have you implemented it as a factory?
things to look out for
Make sure your session factory is a singleton
Create a session as PerWebRequest, using the OpenSession from the SessionFactory
//Setup the Hibernate dependencies
container.AddFacility<FactorySupportFacility>().Register(
Component.For<ISessionFactory>().LifeStyle.Singleton
.Instance(NHibernateHelper.GetSessionFactory()),
Component.For<ISession>().LifeStyle.PerWebRequest
.UsingFactoryMethod(kernel => kernel.Resolve<ISessionFactory>().OpenSession())
);
once you have registerd castle with MVC as a controller factory, it should only create a single session PerWebRequest and only if the controller, or its dependants are dependent on it
I have a sample app if it helps
I use Spring.NET, not Castle Windsor, but I assume the concepts are the same. The scope of your ISession should be defined as per request, and shouldn't actually be created until asked for, as dbones says. While MVC handles every request, asking for a .jpg shouldn't hit a controller which depends on ISession.
Is Castle Windsor eagerly creating per request objects instead of on demand? That may be a configuration issue. Or do you have a custom module, handler or a Global.asax application method that is asking Windsor for an ISession?

Resources