Scott Hanselman

How to allow executable .exe files to be downloaded with ASP.NET Core and UseStaticFiles middleware

November 03, 2020 Comment on this post [9] Posted in ASP.NET | DotNetCore
Sponsored By

In the long process of upgrading this blog and moving it to Azure I've been slowly fixing small bugs and also dealing with the dozens of microsites I have hanging off of hanselman.com. Some were forgotten, and some just keep chugging along. https://www.babysmash.com/ is one of them.

BabySmash is a silly WPF app I wrote 15 years ago for my baby, natch. He's now learning to drive a car, but it still gets thousands of downloads a month. The code is on Github.

The ClickOnce application and manifests hangs off of http://hanselman.com/babysmash, but the /setup.exe returns a 404.

I had assumed that using this simple code in my Startup.cs's Configure() method would enable static files like setup.exe:

app.UseDefaultFiles();
app.UseStaticFiles();

This was naive I suppose as ASP.NET Core is very much locked down by default so you are really encouraged to be specific and there are few unsafe defaults.

UseStaticFiles includes a parameter for options, so I needed to update the list of mappings from extension to mime/type. So this little helper function:

private StaticFileOptions GetStaticFileOptions()
{
var p = new FileExtensionContentTypeProvider();
p.Mappings[".exe"] = "application/octet-stream";
return new StaticFileOptions { ContentTypeProvider = p };
}

and then you pass that StaticFileOptions class into UseStaticFiles.

app.UseStaticFiles(GetStaticFileOptions());

Clearly a "secure by default" decision, but also one that's easily extended for my needs.


Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Raygun.com. Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Using an Xbox Adaptive Controller and Xbox Copilot to get back into gaming after surgery

October 30, 2020 Comment on this post [0] Posted in Gaming
Sponsored By

Xbox Adaptive Controller addonsThanks for the kind words on my post earlier this week. Lots of great tips both in the comments and were sent to me privately. I'm still alternating meds, struggling at physical therapy every day, but I'm fortunate to have my wife (a medical/surgical nurse) here to tell me when it's supposed to hurt and when it's not.

That said, BEFORE the pain started I had a nerve block in my neck and my right side was basically "turned off" for a week. It's definitely preferable to the current pain situation but one of the bright spots is that it give me extreme compassion for those who are missing or unable to use one or more limbs.

I was hoping to use some of my convalescent time to play Xbox but I quickly discovered that it's VERY hard to play a FPS (first-person shooter) with one hand. Of course, tell an engineer that they can't do something and they'll just take it as a challenge, right?

I happened to have an Xbox Adaptive Controller (I bought a few just in case I wanted to set up some relatives or new friends who might need one, never realizing it would be me that needed one) so I hooked it up and tried to figure out how I could game given my paralyzed arm and a lot of free time.

The easiest and clearest way I can example the Xbox Adaptive Controller this - It's an Xbox Controller that pairs and talks to an Xbox like any other controller...but it's not a controller.

It's an Xbox Adaptive Controller Construction Kit.

It has headphone jacks - just 1/8" standard jacks - for every possible input, along with USB ports for Joysticks. You build - adapt - a controller that's ideal for you! It's magical.

Then, combine it with built in Xbox software called Copilot and you can link two controllers so that two can play as one. I was able to use an a standard Xbox Controller in my left hand while using the Xbox Adaptive Controller with my feet. Within an hour I was playing Call of Duty quite competently, I might say.

I even recorded it and put it in my YouTube so I have proof that I got at least one shot in during this round, LOL.

Go learn all about it, the Xbox Adaptive Controller is an extraordinary advice. Then go set it up for a friend or family member, they'll appreciate it very much.

Please take a moment to explore and subscribe to my YouTube. I'm nearing 100k subscribers there and I was told there would be snacks.


Sponsor: Need a multi-cluster load balancer and API gateway? Try VoltMesh: built for modern and distributed apps that require automation, performance and visibility. Start for free today.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

How to deal with Extreme Physical Pain

October 28, 2020 Comment on this post [53] Posted in Musings
Sponsored By

francisco-gonzalez-M8UEJd58GcE-unsplashI'm in a LOT of pain right now. It's hard to say that, especially considering that everyone experiences pain be it emotional or physical. I don't want to make unneeded comparisons or consider my pain as being more important than anyone else's. I'm not burned. I'm not dying of cancer. I am blessed.

But I'm hurting. A lot. It's mine and it's now and it's not clear when it will stop.

It's hard to think. It's hard to move. I can't sleep. Oxycodone makes me feel sick. Advil does nothing.

I've just had my second frozen shoulder surgery (adhesive capsular release) in 4 years. Frozen shoulder is idiopathic (who knows why it happen) and it's known to be quite painful. I can attest that it is. I've been unable to move my right arm for nearly a year. Not just that I couldn't move it, I mean it couldn't be moved by anyone. I couldn't fit the deodorant in to my armpit because the whole joint was hardened.

I had cortisone shots. No result. I finally had formal capsular release surgery where the surgeon goes in there and tidies up, removes scar tissue.

Then the months of physical therapy and forced stretching starts.

I'm going to physical therapy five days a week for an hour a day, and working at home stretching myself for an other 1-2 hours. It's overwhelming and consuming. I just want to be able to pick up a cup from a high shelf. I have basic arm-usage requirements. This is going to be a marathon, and this is the second time this has happened.

Why am I telling you this?

A few reasons. I need the outlet. It's my blog. Because I appreciate you all and you've been here, some of you, for nearly 20 years. Not everything is code.

I had a nerve block in my neck that turned off my right side for a week. That was an extraordinary experience as it was an opportunity to experience a significant, albeit temporary, physical disability. Before I had no ability to move my arm but I had feeling. Now I had zero use of my right arm. It was a numb cadaver arm - dangerously dead weight. I used the time to play Xbox with my feet using the Xbox Adaptive Controller.

This nerve block is wearing off and it's gone from itching, to tingling, to the feeling of an ice pick shoved into my deltoid and armpit every few minutes.

I burst into tears at physical therapy today. The year just hit me all at once. It hurts. Between diabetes and this temporary paralysis, it’s been a week. 2020 is ass. It’s OK. Happens a few times a decade. Maybe it happens to you twice a week. Let it out, listen to your body.

Why am I telling you this?

It's OK to tell people you hurt. You're human. Talk about your pain. Cry. Yell. Sob. Talk some more.

When I'm done yelling, I'm trying to sit quietly and meditate about this pain. What is it trying to tell me? Can I mentally follow the nerve from the location (referred pain or otherwise) to my brain and determine what the body wants me to know? Am I being told there's danger?

I'm finding that there is soft tissue tolerance - what I can handle - and that doesn't always line up with what I'm feeling. I'm feeling near intolerable pain in PT (physical therapy). Like torture with an unknown end date, it's taken me to the level of pain where vomiting is the only escape and then it starts again. However, I persist. I breathe. I try to listen and trust the process and know that if I want to regain the full use of my arms, this is a medically known and studied process. Physical therapy works if you do it.

The cognitive dissonance is overwhelming. Your body says you're actively dying but your conscious brain can - must - override it and let the pain flow freely. You observe it, rather than obstruct it.

I hate this process but I'm going to learn from it. I'm learning and listening to my body and how I react to something so extreme.

The pain is important to acknowledge because this pain is gonna make me better and stronger. But it still hurts. Here we go.

I hope that you, Dear Reader, are not in pain. But if you are, I hope it passes and that you come out better on the other side. I'm going to use this Bad Input for Good.

BTW: Thanks to Volterra for sponsoring the blog this week. I suspect they didn't know what blog post(s) their ad would land on, but I appreciate their support and understanding as not every blog post is about code. This one is about people and their pain. Give them a click.


Sponsor: Need a multi-cluster load balancer and API gateway? Try VoltMesh: built for modern and distributed apps that require automation, performance and visibility. Start for free today.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Using the ASP.NET Core Environment Feature to manage Development vs. Production for any config file type

October 22, 2020 Comment on this post [4] Posted in ASP.NET
Sponsored By

ASP.NET Core can understand what "environment" it's running under. For me, that's "development," "test," "staging," "production," but for you it can be whatever makes you happy. By default, ASP.NET understand Development, Staging, and Production.

You can the change how your app behaves by asking "IsDevelopment" to do certain things. For example:

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
{
app.UseExceptionHandler("/Error");
}

There are helpers for the standard environments, or I can just pass in a string.

You can also make Environmental decisions with taghelpers like this in your Views/Razor Pages. I did this when I dynamically generated my robots.txt files:

@page
@{
Layout = null;
this.Response.ContentType = "text/plain";
}
# /robots.txt file for http://www.hanselman.com/
User-agent: *
<environment include="Development,Staging">Disallow: /</environment>
<environment include="Production">Disallow: /blog/private
Disallow: /blog/secret
Disallow: /blog/somethingelse</environment>

This is a really nice way to include things like banners or JavaScript when your site is running in a certain environment. These are easily set as environment variables if you're running in a container. If you're running in an Azure App Service you set the environment from the Config blade:

Now that I've moved this blog to Azure, we have a number of config files that are specific to this blog. Since the configuration features of ASP.NET are so flexible it was easy to extend this idea of environments to our own config files.

Our Startup class sets up the filesnames of our various config files. Note the second line, if we have no environment, we just look for the regular file name.

public Startup(IWebHostEnvironment env)
{
hostingEnvironment = env;

var envname = string.IsNullOrWhiteSpace(hostingEnvironment.EnvironmentName) ?
"." : string.Format($".{hostingEnvironment.EnvironmentName}.");

SiteSecurityConfigPath = Path.Combine("Config", $"siteSecurity{envname}config");
IISUrlRewriteConfigPath = Path.Combine("Config", $"IISUrlRewrite{envname}config");
SiteConfigPath = Path.Combine("Config", $"site{envname}config");
MetaConfigPath = Path.Combine("Config", $"meta{envname}config");
AppSettingsConfigPath = $"appsettings.json";

...

Here's the files in my Visual Studio. Note that another benefit of this naming structure is that the files nest nicely underneath their parent file.

Nested config files

The formalization of environments is not a new thing, but the adoption of it deeply into our application at every level has allowed us to move from dev to staging to production very easily. It's very likely that you have done this in your application, but you may have rolled your own solution. Take a look if you can remove code and adopt this built in technique.

Here's some articles I've already written on the subject of moving this blog to the cloud:

If you find any issues with this blog like

  • Broken links and 404s where you wouldn't expect them
  • Broken images, zero byte images, giant images
  • General oddness

Please file them here https://github.com/shanselman/hanselman.com-bugs and let me know!


Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Raygun.com. Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Don't ever break a URL if you can help it

October 21, 2020 Comment on this post [12] Posted in DasBlog | Open Source
Sponsored By

404Back in 2017 I said "URLs are UI" and I stand by it. At the time, however, I was running this 18 year old blog using ASP.NET WebForms and the URL was, ahem, https://www.hanselman.com/blog/URLsAreUI.aspx

The blog post got on Hacker News and folks were not impressed with my PascalCasing but were particularly offended by the .aspx extension shouting "this is the technology this blog is written in!" A rightfully valid complaint, to be clear.

ASP.NET has supported extensionless URLs for nearly a decade but I have been just using and enjoying my blog. I've been slowly moving my three "Hanselman, Inc" (it's not really a company) sites over to Azure, to Linux, and to ASP.NET Core. You can actually scroll to the bottom of this site and see the git commit hash AND CI/CD Build (both private links) that this production instance was built and deployed from.

As tastes change, from anglebrackets to curly braces to significant whitespace, they also change in URL styles, from .cgi extesnions, to my PascalCased.aspx, to the more 'modern' lowercased kebab-casing of today.

But how does one change 6000 URLs without breaking their Google Juice? I have history here. Here's a 17 year old blog post...the URL isn't broken. It's important to never change a URL and if you do, always offer a redirect.

When Mark Downie and I discussed moving the venerable .NET blog engine "DasBlog" over to .NET Core, we decided that no matter what, we'd allow for choice in URL style without breaking URLs. His blog runs DasBlog Core also and applies these same techniques.

We decided on two layers of URL management.

  • An optional and configurable XML file in the older IIS Rewrite format that users can update to taste.
    • Why? Users with old blogs like me already have rules in this IISRewrite format. Even though I now run on Linux and there's no IIS to be found, the file exists and works. So we use the IIS Rewrite Module to consume these files. It's a wonderful compatibility feature of ASP.NET Core.
  • The core/base Endpoints that DasBlog would support on its own. This would include a matrix of every URL format that DasBlog has ever supported in the last 10 years.

Here's that code. There may be terser ways to express this, but this is super clear. With or without extension, without or without year/month/day.

app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/healthcheck");

if (dasBlogSettings.SiteConfiguration.EnableTitlePermaLinkUnique)
{
endpoints.MapControllerRoute(
"Original Post Format",
"~/{year:int}/{month:int}/{day:int}/{posttitle}.aspx",
new { controller = "BlogPost", action = "Post", posttitle = "" });

endpoints.MapControllerRoute(
"New Post Format",
"~/{year:int}/{month:int}/{day:int}/{posttitle}",
new { controller = "BlogPost", action = "Post", postitle = "" });
}
else
{
endpoints.MapControllerRoute(
"Original Post Format",
"~/{posttitle}.aspx",
new { controller = "BlogPost", action = "Post", posttitle = "" });

endpoints.MapControllerRoute(
"New Post Format",
"~/{posttitle}",
new { controller = "BlogPost", action = "Post", postitle = "" });

}
endpoints.MapControllerRoute(
name: "default", "~/{controller=Home}/{action=Index}/{id?}");
});

If someone shows up at any of the half dozen URL formats I've had over the years they'll get a 301 permanent redirect to the canonical one.

UPDATE: Great tip from Tune in the comments: "After moving several websites to new navigation and url structures, I've learned to start redirecting with harmless temporary redirects (http 302) and replace it with a permanent redirect (http 301), only after the dust has settled…"

The old IIS format is added to our site with just two lines:

var options = new RewriteOptions().AddIISUrlRewrite(env.ContentRootFileProvider, IISUrlRewriteConfigPath);
app.UseRewriter(options);

And offers rewrites to everything that used to be. Even thousands of old RSS readers (yes, truly) that continually hit my blog will get the right new clean URLs with rules like this:

<rule name="Redirect RSS syndication" stopProcessing="true">
<match url="^SyndicationService.asmx/GetRss" />
<action type="Redirect" url="/blog/feed/rss" redirectType="Permanent" />
</rule>

Or even when posts used GUIDs (not sure what we were thinking, Clemens!):

<rule name="Very old perm;alink style (guid)" stopProcessing="true">
<match url="^PermaLink.aspx" />
<conditions>
<add input="{QUERY_STRING}" pattern="&amp;?guid=(.*)" />
</conditions>
<action type="Redirect" url="/blog/post/{C:1}" redirectType="Permanent" />
</rule>

We also always try to express rel="canonical" to tell search engines which link is the official - canonical - one. We've also autogenerated Google Sitemaps for over 14 years.

What's the point here? I care about my URLs. I want them to stick around. Every 404 is someone having a bad experience and some thoughtful rules at multiple layers with the flexibility to easily add others will ensure that even 10-20 year old references to my blog will still resolve!

Oh, and that article that they didn't like over on Hacker News? It's automatically now https://www.hanselman.com/blog/urls-are-ui so that's nice, too!

Here's some articles I've already written on the subject of moving this blog to the cloud:

If you find any issues with this blog like

  • Broken links and 404s where you wouldn't expect them
  • Broken images, zero byte images, giant images
  • General oddness

Please file them here https://github.com/shanselman/hanselman.com-bugs and let me know!


Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Raygun.com. Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.