How do you tell Nancy to redirect to a URL inside a virtual directory? - iis-7.5

I'm running a Nancy application in a Virtual Directory on my development machine and I've got static content working but if the module redirects the browser then the URL being sent back is incorrect.
If the result is:
return new RedirectResponse("/");
Then the browser redirects to http://localhost/ rather than the root of the virtual directory.
If I try
return new RedirectResponse("~/");
I get taken to http://localhost/MyVirtualDirectory/action/~/ which at least includes the virtual directory, but the rest is screwed up.
I should point out that the module is created like this...
public abstract class ActionRootModule : NancyModule
{
protected ActionRootModule() : base("/action/") { }
}
public class SendEmailModule : ActionRootModule
{
public SendEmailModule()
{
// Parts missing for brevity....
Post["/send-email/"] = o => PostSendEmail(o);
}
private dynamic PostSendEmail(dynamic o)
{
// Do stuff...
return new RedirectResponse("~/");
}
}
What is the correct way of telling Nancy to redirect to a specific URL inside a virtual directory?
I'm running Visual Studio on Windows 7 and IIS 7.5 (not IIS Express - as I have incoming traffic from third parties making callbacks)
This won't be a issue on production as the production deployment won't be in a virtual directory.

There are two ways to solve this.
1) Use return Response.AsRedirect("~/");
2) Use return new RedirectResponse(Context.ToFullPath("~/"));
The first option is just a convenient wrapper around the second so either way yields the same result (https://github.com/NancyFx/Nancy/blob/85dfe8567d794ff3e766521a9fa0891832d4fc8a/src/Nancy/FormatterExtensions.cs#L51). The call to ToFullpath() is what corrects the /~/ you saw in your redirected URL.

Related

asp.net mvc area not working on iis

I crated area/modular MVC5 application according to this tutorial:
This
it worked perfectly on local. but i got 404 error, when i deployed project on iis and clicked on specified area link.
and i didn't find any solution for that.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Sale
and this is my action links which are perfectly working on local run:
#Html.ActionLink("Sale","Index","Sale",new { Area="Sale"},null)
edited:
public class SaleAreaRegistration:AreaRegistration
{
public override string AreaName
{
get
{
return "Sale";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Sale_default",
"Sale/{controller}/{action}/{id}",
new { controller = "Sale", action = "Index", id = UrlParameter.Optional },
new string[] { "MVCPluggableDemo.Controllers" }
);
}
}
attention: according to the tutorial which i said in first line. i created my module project in another virtual project in solution(the real path is in area folder of main project like"~/ProjectName/Areas/"). but i think the publisher does't understand it...because i don't see any of my module views in deployed path.
its better to completing my help request by this obvious question:
How to deploy MVC project include areas, and the modules which are in areas folder?
Its simply obvious:
Check your page directory. The server cannot locate your the file,
it maybe in different folder.
Also try to Check this.
the main Reason of my problem was this:
VisualStudio doesn't include my Area folder, in publish path..why? i don't know
Solution: no solution yet!..but i added my module dll inside bin folder manually. and created my areas folder and his modules projects solution(dlls' are not not necessary) in main published solution, manually to ..
finally it worked, but i dont thing is the standard solution

Serving static files in ASP.NET 5 MVC 6

My wwwroot static files aren't being resolved.
I understand that to serve static files, I need to put them in wwwroot:
favicon.ico resolves just fine, but schema/v1-0.json does not. I get the generic message:
The resource you are looking for has been removed, had its name
changed, or is temporarily unavailable.
I have the following wired up in Startup:
app.UseMiddleware<StaticFileMiddleware>(new StaticFileOptions());
app.UseStaticFiles();
I am using DNX beta6. The above require beta5 packages. I cannot find anything online regarding serving static files in beta6. I am not sure if this could be the cause of the problem.
EDIT:
As per Sirwan's answer, I have added the following, but the json file is still not available:
var options = new StaticFileOptions
{
ContentTypeProvider = new JsonContentTypeProvider(),
ServeUnknownFileTypes = true,
DefaultContentType = "application/json"
};
app.UseStaticFiles(options);
The JsonContentTypeProvider class:
public class JsonContentTypeProvider : FileExtensionContentTypeProvider
{
public JsonContentTypeProvider()
{
Mappings.Add(".json", "application/json");
}
}
I can even see the file when browsing the server:
Try this:
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "image/x-icon"
});
If you have multiple file types that are unknown to ASP.NET you can use FileExtensionContentTypeProvider class:
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".json", "application/json");
provider.Mappings.Add(".ico", "image/x-icon");
// Serve static files.
app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });
If you're using IIS, make sure you've added the correct mime-type mappings if you don't have a catch-all managed handler. Even though you don't need web.config for your website to work, IIS will still use that for your website.
Someone correct me if I'm wrong, but I believe that if you have not configured IIS to use a managed handler to serve static files it will still default to StaticFileModule and calling app.UseStaticFiles doesn't actually do anything. If you run it using dnx, however, then app.UseStaticFiles gets used.
Just a side note, you should probably also upgrade to beta7 if you haven't already.

Nancy on Owin doesn't serve static content

I'm running self hosted Nancy web application on Owin and have troubles with static content.
Let's say my application runs from this folder:
c:/myfolder/
My Views are in here:
c:/myfolder/Manager/Views/
so in my browser I can go to http://localhost:85/Manager and my page loads.
I simply can't make it to serve static content though, all my files are in /Content folder, I tried to place it both to /myfolder and /Manager folder with no luck.
Neither http://localhost:85/Manager/Content/css/styles.css nor http://localhost:85/Content/css/styles.css urls work
How do I get it to work?
Fixed the problem by adding these lines of code to Startup :
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
...
var options = new FileServerOptions()
{
RequestPath = PathString.Empty,
FileSystem = new PhysicalFileSystem("/Path/here")
};
app.UseFileServer(options);

Force Application Start on Azure Web Role

I have a web role on azure and I would like to force a Application_Start without waiting for the first request.
I managed to set the "Start Automatically" property to true on my site
AutoStart a WCF on Azure WebRole
But the Application_Start is not called until the first request comes.
I don't know exactly if I am missing something important here. The server is a W2008 R2 and the IIS version is 7.5
Thanks!
SOLUTION
I put the solution code here. I hope will help someone. I just added a WebRole.cs and just put that code to perform a ping every 30 seconds. Please netice I'm browsing Service.svc because this is my endpoint, your endpoint could be another one. Notice I'm asking for "Endpoint1". If you have more than one endpoints, you should review that line.
public class WebRole : RoleEntryPoint
{
public override void Run()
{
var localuri = new Uri( string.Format( "http://{0}/Service.svc", RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint ) );
while (true)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(localuri);
request.Method = "GET";
var response = request.GetResponse();
}
catch { }
System.Threading.Thread.Sleep(30000);
}
}
public override bool OnStart()
{
return base.OnStart();
}
}
The IIS will only start when the first request arrives. The workaround is to send an HTTP request to the same VM from within OnStart or your RoleEntryPoint descendant - that's easy using WebRequest or equivalent class.
Jordi, I've recently experienced the same issue.
Based on my test Application_Start() is called ONLY when the 1st request ISS for the WebApp. (if you try to start VS in Debug without it open any page (see options in proj/debug), you will see that Application_Start() won't be called also if you don't run the WebApp in Azure)
I suppose that you need doing somethings when the WebRole start, well put your code in the WebRole.cs ;)
Here you can override OnStart() and OnStop() and put your code that wiil be execuded when the WebRole will start.
I've used this way to run a BakgroundWorker that do some scheduled tasks, independently from IIS.
I hope this help.
Davide.
Note:
1 - if you dont'have a WebRole.cs create it in the root of project and write inside:
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
...your code...
return base.OnStart();
}
}
2 - If you need to debug the code mind that you need to run VS in debug with the Azure project that refer to WebApp as a "Run Project", otherwise the WebRole will not be called
You could try putting some code in your WebRole.cs to request some URLs from your website. I've tried that, and it seems to work somewhat. But it's a pain to debug, so I never got it really nailed down.
Another option would be to use IIS Application Initialization. You can't use it on IIS 7.5, but you can get IIS 8 if you upgrade your roles to Windows 2012 (set osFamily="3" in your .cscfg).

Strange behavior of Windows Azure Compute Emulator (SDK 1.8) with multiple role instances on a clean machine with VS2012 but WITHOUT VS2010

Have you ever tried to run a hosted service in the windows azure emulator with full IIS and multiple role instances? Some days ago I noticed that only one of the multiple instances of a web role is startet in IIS at a time. The following screenshot illustrates the behavior and the message box in front of the screenshot shows the reason for this behavior. The message box appears on trying to start one of the stopped websites in the IIS Manager.
Screenshot: IIS with stopped Websites
The sample cloud application contains two web roles: MvcWebRole1 and WCFServiceWebRole1 each configured to use three instances. My first thought was: "Sure! No port collision will happen in the real azure world because every role instance is an own virtual machine. It cannot work in the emulator!" But after some research and analyzing many parts of the azure compute emulator I found out that the compute emulator creates a unique IP for each role instance (in my example from 127.255.0.0 up to 127.255.0.5). This MSDN blog article (http://blogs.msdn.com/b/avkashchauhan/archive/2011/09/16/whats-new-in-windows-azure-sdk-1-5-each-instance-in-any-role-gets-its-own-ip-address-to-match-compute-emulator-close-the-cloud-environment.aspx) of the microsoft employee Avkash Chauhan describes this behavior as well. After that conclusion I came to the following question: why the hell does the compute emulator (more precisely DevFC.exe) not add the IP of the appropriate role to the binding information of each Website???
I added the IP to each Website by hand and tadaaaaa: every Website can be started without any collisions. The next screenshot demonstrates it with the changed binding information highlighted.
Screenshot: IIS with started Websites
Once again: Why the hell does the emulator not do it for me? I wrote a small static helper method to do the binding extension thing for me on every role start. Maybe someone wants to use it:
public static class Emulator
{
public static void RepairBinding(string siteNameFromServiceModel, string endpointName)
{
// Use a mutex to mutually exclude the manipulation of the iis configuration.
// Otherwise server.CommitChanges() will throw an exeption!
using (var mutex = new System.Threading.Mutex(false, "AzureTools.Emulator.RepairBinding"))
{
mutex.WaitOne();
using (var server = new Microsoft.Web.Administration.ServerManager())
{
var siteName = string.Format("{0}_{1}", Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
var site = server.Sites[siteName];
// Add the IP of the role to the binding information of the website
foreach (Binding binding in site.Bindings)
{
//"*:82:"
if (binding.BindingInformation[0] == '*')
{
var instanceEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[endpointName];
string bindingInformation = instanceEndpoint.IPEndpoint.Address.ToString() + binding.BindingInformation.Substring(1);
binding.BindingInformation = bindingInformation;
server.CommitChanges();
}
else
{
throw new InvalidOperationException();
}
}
}
// Start all websites of the role if all bindings of all websites of the role are prepared.
using (var server = new Microsoft.Web.Administration.ServerManager())
{
var sitesOfRole = server.Sites.Where(site => site.Name.Contains(RoleEnvironment.CurrentRoleInstance.Role.Name));
if (sitesOfRole.All(site => site.Bindings.All(binding => binding.BindingInformation[0] != '*')))
{
foreach (Site site in sitesOfRole)
{
if (site.State == ObjectState.Stopped)
{
site.Start();
}
}
}
}
mutex.ReleaseMutex();
}
}
}
I call the helper method as follows
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
if (RoleEnvironment.IsEmulated)
{
AzureTools.Emulator.RepairBinding("Web", "ServiceEndpoint");
}
return base.OnStart();
}
}
I got it!
I have this behavior on three different machines which are all formatted and served with fresh clean windows 8, Visual Studio 2012 and Azure SDK 1.8 and Azure Tools installations recently. So a reinstallation of the Azure SDK and Tools (as Anton suggests) should not change anything. But the cleanliness of my three machines is the crucial point! Anton, do you have Visual Studio 2010 on your machine with at least VS2010 SP 1 installed? I analyzed IISConfigurator.exe with ILSpy and found the code which sets the IP in the binding information of the websites to '*' (instead of 127.255.0.*). It depends on the static property Microsoft.WindowsAzure.Common.Workarounds.BindToAllIpsWorkaroundEnabled. This method internally uses Microsoft.WindowsAzure.Common.Workarounds.TryGetVS2010SPVersion and leads to setting the IP binding to '*' if the SP level of Visual Studio 2010 is smaller than 1. TryGetVS2010SPVersion checks four registry keys and I don't know why but one of the keys exists in my registry und returns the Visual Studio 2010 SP level 0 (I never installed VS2010 on no one of the three machines!!!). As I changed the value of HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\DevDiv\vs\Servicing\10.0\SP from 0 to 10 (something greater 0 should do it) the Azure Emulator starts to set the 127.255.0.* IPs of the roles to the binding information on all of the websites in the IIS and all websites are started correctly.

Resources