Keep URL extensions - asp.net

I know that removing URL extensions is the new model for website programming. Unfortunately, my site is hosted on a hybrid server configuration. The call to my site goes into an Apache server that recognizes that my call is for a .aspx page, and passes the call along to an IIS server to complete the call. This complicates my website at this point because I am coding in Visual Studio 2015, and it models after the new rules of removing the extensions, and the call is never passed along to the IIS server.
I am not a big HTML guy, and I cannot find anything to place in web.config or my global.asax file for code to tell the system to overwrite the rule of removing the extension, and to keep my extensions. I have seen several posts here to remove the extensions, but nothing to keep them.
Basically, when I call www.mysite.com/Default.aspx, the current config removes the .aspx extension, and the call is for www.mysite.com/Default. I want to KEEP the .aspx extension on the call to the site so that it passes through the Apache server and to the IIS server. Any help would be greatly appreciated.

Have a look in your App_Start directory for a class called RouteConfig.cs and disable AutoRedirectMode using this line of code
settings.AutoRedirectMode = RedirectMode.Off;
This is what automatically removes extensions from your web pages.
Full example below...
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
}

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.

Adding Cache headers via a Web.config for specific locations only

We have an application that has been developed by the third party, and I don't want to go back to them to get them to add in cache control for specific pages.
All the pages that need caching disabled are in a single directory.
The issue is that IE seems to not follow Cache-control:nocache properly, so we need to add in Pragma:nocache and cache age as well.
Is there a way to do this using configs in the directory? will it cascade through all child directories? Can it be done via the main web.config?
To be clear, I'm not looking for a way to do this via code, it needs to be via configuration of either IIS or the web.config files.
We're using ASP.NET 2.0 and 4.0, on IIS 6.0.
This can be done in IIS using the UI, it's actually quite easy, or atleast it was in my use case.
All you do is simply open up IIS manager, navigate to the site and then the directory you want to add the headers to Right Click -> properties.
Click the "Headers" tab, and add in the headers you require.
This goes recursively down the child directories, and adds the headers before any added by the code.
In IIS 7.0/7.5, you can use the StaticContent section of a web.config in each of the directories.
You can do that on global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string cTheFile = HttpContext.Current.Request.Path;
if (cTheFile.Contains("/ExtraDir/"))
{
// add your header here
app.Response.AppendHeader("Pragma", "no-cache");
}
//... rest code of...
}

Configure IIS7 to server static content through ASP.NET Runtime

I searched high an low and still cannot find a definite answer.
How do I configure IIS 7.0 or a Web Application in IIS so that ASP.NET Runtime will handle all requests -- including ones to static files like *.js, *.gif, etc?
What I'm trying to do is as follows.
We have kind of SaaSy site, which we can "brand" for every customer. "Branding" means developing a custom master page and using a bunch of *.css and other images.
Quite naturally, I'm using VirtualPathProvider, which operates like this:
public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
{
if(PhysicalFileExists(virtualPath))
{
var virtualFile = base.GetFile(virtualPath);
return virtualFile;
}
if(VirtualFileExists(virtualPath))
{
var brandedVirtualPath = GetBrandedVirtualPath(virtualPath);
var absolutePath = HttpContext.Current.Server.MapPath(brandedVirtualPath);
Trace.WriteLine(string.Format("Serving '{0}' from '{1}'",
brandedVirtualPath, absolutePath), "BrandingAwareVirtualPathProvider");
var virtualFile = new VirtualFile(brandedVirtualPath, absolutePath);
return virtualFile;
}
return null;
}
The basic idea is as follows: we have a branding folder inside our webapp, which in turn contains folders for each "brand", with "brand" being equal to host name. That is, requests to http://foo.example.com/ should use static files from branding/foo_example_com, whereas http://bar.example.com/ should use content from branding/bar_example_com.
Now what I want IIS to do is to forward all requests to static files to StaticFileHandler, which would then use this whole "infrastructure" and serve correct files. However, try as I might, I cannot configure IIS to do this.
II7 already does that if the application pool's Managed Pipeline Mode is set to Integrated which is the default. In Integrated mode, ASP.NET handles all requests including those for static objects.
If you have to leave your application pool in Classic Mode then you need to use the same techniques you would use in IIS 6 to explicitly create handlers for the various static extensions.
Additional Information Based on Comments: I think your missing piece is creating an HttpHandler to handle the other extensions (.js, .css, etc.). Without this, then ASP.NET will use the default handling for these types of files. You would create a reference to you handler in your web.config. This article is an example of creating an HttpHandler for static files.
Kudos to everyone, but the problem was in totally different space.
VirtualPathProvider cannot be used in a pre-compiled web site. I'm furious.

How to consume different proxyclass versions (production or test) of ASMX webservice

I've got an ASMX webservice as a separate project in Visual Studio 2005. In pursuit of "assembly separation" per a CODE Magazine tutorial, my proxy class is in a separate class library project containing no code of mine - just a web reference named ASMXproxy with the associated reference.cs, app.config, .disco and .wsdl files. Thus, when compiled I have a FileServiceProxy.dll.
For consuming this WS, I have a web app project called FileServiceDemo in this same solution. It has no web reference but instead a "regular" reference to FileServiceProxy.dll. In my default.aspx.cs file, I gain access to my WS via these snippets:
using FileServiceProxy.ASMXproxy;
public partial class _Default : System.Web.UI.Page
{
ASMXproxy.FileService brokerService;
protected void Page_Load(object sender, EventArgs e)
{
try
{
brokerService = new ASMXproxy.FileService();
So while things work OK this way, I find it awkward when I want to test a deployed version or make changes to a "localhost" version. I can't simply make changes to the app.config:
<applicationSettings>
<FileServiceProxy.Properties.Settings>
<setting name="FileServiceProxy_ASMXproxy_FileService" serializeAs="String">
<value>http://localhost/TRIMBrokerService/FileService.asmx</value>
</setting>
</FileServiceProxy.Properties.Settings>
</applicationSettings>
In short, when I need to publish my web app to another server, I have to change the web reference in proxy class and rebuild it. Then when I want to debug it on my localhost, I have to change the web reference back to localhost (as above).
Ideally, I would like to expose some sort of choice (e.g. radio buttons or a textbox for altering a URL at runtime) in my web app demo project such that I could have a "late binding" of sorts for the desired FileServiceProxy.dll to be used at runtime. Others have sketched proposals "using config files" but I am stuck on how to do that. It appears to me that I would have to have an additional project and hence another DLL - perhaps FileServiceProxyPROD.dll - but this seems awkward and even then I'm not sure what else I'd have to do.
Actually, you can use the same reference. Just change the Url property of the proxy instance:
using (var svc = new WebServiceProxy())
{
svc.Url = realUrl;
var result = svc.ServiceMethod();
}

asp.net site default document in subfolder

My default document is in subfolder not in root how can i make it default in asp.net 2.0 website.
Tried iis7 default document setting to '/pages/default.aspx'
'~/pages/default.aspx' but it didn't work.
Default document is not the same as start page. Default document means if I requested mysite.com/somefolder and didn't specify a file, which file should IIS display.
If you want to use a specific page as your home page, create a Default.aspx file and write this in it's codebehind class:
public override void ProcessRequest(HttpContext context) {
context.Response.Redirect("pages/default.aspx", true);
}
As the client might have disabled Javascript, a server side approach would be more reliable. However it's best to issue a permanent redirect instead of a simple Response.Redirect. Also doing it using JS will be bad from a SEO point of view.
You don't need to create a dummy Default.aspx page.
In your Global.asax.cs file, write the following:
public void Application_Start(object sender, EventArgs e)
{
var routeCollection = RouteTable.Routes;
routeCollection.MapPageRoute("DefaultRoute", string.Empty, "~/YourDesiredSubFolder/YourDesiredDocument.aspx");
}
Explanation:
Application_Start code is guaranteed to run once and only once on the application start.
The first line of code, gets a collection of the URL routes for your application.
The second line of code, defines a new route pointing to your inner page in the subfolder that you wish.
The second argument is empty to indicate that this route is used when there's no specific page is requested and there's no Default document existing.
Default documents are a subfolder-specific thing - what you're trying to do won't (directly) work. Set up a default.htm file in the root, and have it refresh to your real "home page".
The better question you should be asking is how on Earth your homepage got out of the root directory.
In theory you could have a Web.config file inside the directory and use the defaultDocument element to set the default document. See here: https://stackoverflow.com/a/2012079/125938.
Unfortunately I haven't been able to get it to work myself locally, but that might be because it isn't supported in the Visual Studio development server.
Say "index.html" is the default page you want and it is present in "Public" subfolder.
Instead of specifying "/Public/index.html" as the default site, try "Public/index.html"

Resources