Get URL to website from within Application_Start? - asp.net-mvc

Hi,
I need to generate a correct URL to a action in my APS.NET MVC 3 website and this have to be done in the Application_Start in Gloabl.asax.
I have tried to set a UrlHelper but the HttpContext.Current is always null?
The adress i look for is somthing like this : http://www.mysite.se/MyController/MyAction.
I have also tried VirtualPathUtility.ToAbsolute("~/") but that will only give me "/mysite/"

I'm afraid you can't access the HttpContext in the Application_Start when the application is running in the IIS integration mode (you can achieve that in the classic mode but that's not recommended).
Mike describes here a workaround how to do that in Application_BeginRequest such that your code still runs only for the first request not for the later ones.

Related

How can I protect an old site that has no validation?

Scenario:
I have a website (CATALOG.COM), without any validation request, and without source code.
Now I need to set a validation to access.
Possibility Solution
If I create another site with validation (START_WEB.Com), and after I redirect on CATALOG.COM, can be a good solution ?
For example:
[Authorize]
public ActionResult Run_Old_WebSite()
{
return Redirect("www.Catalog.com");
}
Any suggestions?
p.s. I work with Asp.net, Mvc and Bootstrap
without the source code availability , You cannot validate the page.
Below is the alternate solution for this:
When any one hit the CATALOG.COM apply the 301 redirection rule to it and return it to the new site url like: START_WEB.Com?returnUrl=CATALOG.COM
Then in the new site add some validation login page, authenticate it and then redirect it to the old url: CATALOG.COM?validated=true
In this way you can achieve this, Here you can track the users also who are visiting to the old url site.
Assuming you have access to the published files, I would copy them to my local machine, decompile the site specific assemblies using something like Jetbrains dotPeek (it's free) and re-create a new solution in Visual Studio with the decompiled sources. Depending on site size/complexity this may take a bit of time but you would at least be able to make improvements to the code,build and deploy.

Calling Web API from MVC Application when both in same Solution

So I am totally new to the Web API and I thought to myself I would start a test project to learn it and AngularJS and how they can work together nicely ... etc.
I have created a multi-tier architecture within my solution, and inside my TPlan.API project, I have the following route configured in the Global.asax
GlobalConfiguration.Configuration.Routes.Add("default",
new HttpRoute("api/{controller}"));
TPlan.WEB is my MVC application, and it is set up as "Start Up Project". When I run it, I want to be able to go to:
mysite:port/api/test
And get the value from the API from my test controller in there.
However it is not working for me, I get the following:
No HTTP resource was found that matches the request URI 'mysite:port/api/test'.
Er, in visual studio, right click on the solution and go to properties.
Go to startup and select the "multiple projects" option and tick the website and the webservice to both start up.
That will start both of them. But as has been pointed out, they're running on separate ports, they're completely separate applications...
I don't think you can register a route that belongs outside of the website.
We work like this
View => POST / GET => MVC Controller => HTTP Request => API Controller
So our mvc views post or get to our mvc controllers, and then we fire off a separate http request to the web api. this is a server to server call, its like calling any external web service. we wait for the response from the api and then return whatever is required to the view...
What you are attempting isn't logically possible without installing your WebAPI project into IIS ahead of time, which I'm sure is not what you want. Both projects cannot be run at the same time, as only one project will be able to launch a debug session of IIS Express. Even if both projects were able to run at the same time, they would be on different logical ports, and routing from one project would have to be manually sent to the listening port of the other instance. Basically, your Global.asax on your API project will never run, as that project will be built as a class library.
Make your TPlan.API project a simple Assembly, reference it from TPlan.Web and pass the GlobalConfiguration.Configuration property over to a Register method that is in your API assembly. When the MVC project starts, both the MVC routes and the Web Api routes will be active on the same web host.
Here is an example of an API that has both a console host and a web host in the same solution.
Please check the following site. I believe the issue lies in the configuration of the route
http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api

Can't Access elmah.axd When Running ASP.net MVC on Apache

I'm using MVC2, mono 2.10.5, mod-mono-server4.
ELMAH is configured as per here, but I am using the XML file logger.
When trying to access /elmah.axd locally I get the following message in error log (indicating that the logging capabilities for ELMAH are working):
The controller for path '/elmah.axd' was not found or does not implement IController.
The following is still present in Global.asax's RegisterRoutes method as well:
routes.IgnoreRoute ("{resource}.axd/{*pathInfo}");
It seems like adding the handlers for ELMAH is not working with apache - it is working on a windows 7 desktop with cassini, haven't tried with IIS.
I can post more details if needed.
This may not be the best answer, but I've followed the directions here to display ELMAH information in a more MVC-friendly way (this makes it easier to allow remote access as well). I'm running into another mono-specific error now, but I will post that as a new question.

MVC: "The incoming request does not match any route." - with a default request

I am getting this error only SOMETIMES (apparently randomly, though I know there's really no such thing in computers).
I'm working on an MVC (release 1) site in VS 2008. The machine is running Windows 7 with IIS 7, but this instance is running under the Webserver built into VS (Cassini?). When I do an F5 to start running it, I will sometimes get an exception trap at the 4th line of the Page_Load method of Default.aspx.cs:
httpHandler.ProcessRequest(HttpContext.Current);
The exception method is:
The incoming request does not match any route.
Examining the HttpContext.Current.Request object shows a path of "/", and a rawUrl of "/Default.aspx?" - i.e, your normal empty request to a site.
The MapRoutes routine is pretty much standard except for a couple of IgnoreRoutes lines (ignore "pics/{*imgfile}", and ignore "{*favicon}"). The Application_Start routine has configuration for a StructureMap, and a call to SetControllerFactory before the call to RegisterRoutes, but it otherwise standard.
Usually, if I restart VS, the problem goes away. (And almost always if I restart the machine).
Any clues?
Thanks.
Set a default Page. F5 in asp.net will launch to whatever page is selected. However since in MVC, your View may or may not have an associated controller function, it can be trying to call an action that doesn't exist. Open up your project properties and set the debug start page to your site home controller action.
Sorry my poor english ...
I had this problem too! Misteriously the method Application_Start that call the routes disappeared in the Global.asax!!
vb.net:
Sub Application_Start()
RegisterRoutes(RouteTable.Routes)
End Sub
put this method in global.asax if it doesn't exist

ASP.NET MVC Deployment Issues

HI am new to ASP.NET MVC and I am running into some issues trying to deploy a very simple site! I took the MVC Storefront (Kona), made some basic changes and now I would like to deploy it to a webserver! For testing, I just turned on IIS7 on my windows 7 desktop! I change the web projects settings to use local IIS and run it!
The first issue I get is the "Request is not available in this context"... To fix this I had to remove the HttpContext.Current.GetSiteUrl() in the global.asx.cs file! If I now run the app it works but I have no css? If I switch back to VS development server everything works great?
I can also swithc from integrated mode on IIS to classic which makes the css work but then I have no routing?
On IIS 7, HttpContext.Current is null in Application_Start because it isn't triggered by the first request anymore.
You can work around that issue by moving the code from Application_Start to Application_BeginRequest and add a flag to indicate if the application is already initialized (member of the Global class). When false, execute the code that uses HttpContext.Current and set the flag to true so subsequent requests don't execute that piece of code.
Doing what is said above fixed the css for me but adding this to
http://blogs.msdn.com/mikeormond/archive/2008/11/06/and-getting-asp-net-routing-up-and-running-in-web-applications-and-on-iis7.aspx
Fixed the routing for me.
Good luck took me long enough!

Resources