Custom config section error on file download in MVC

336 Views Asked by At

I'm using a custom configuration section in my web.config in an ASP.NET MVC 5 project. It works perfectly fine, until I try to download a file via a link:

<a href="@Url.Action("Download", new { id = Model.Id })">...

Here's how I'm handling the download in the controller:

return new FileStreamResult(myStream, "application/octet-stream")
{
    FileDownloadName = "MyFile.someExt"
}

I will reiterate that my custom config works fine, except when I click the download link mentioned above. I then get an error:

Unrecognized element 'link'.

link being a child element of my custom config section, i.e.,

<navigationMenu>
    <link ...

Edit 1: It may be useful info that I'm accessing my custom config section in this manner:

XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
    .Root.Element("navigationMenu");

However, it is properly registered in the <configSections /> of the web.config, i.e.,

<!-- From web.config -->
<section name="navigationMenu" type="MySite.Helpers.NavigationMenuSection, MySite" />

// With the config section class declared like so:
public class NavigationMenuSection : ConfigurationSection
{ }

But i'll stress again that this works fine, until I click the download link. Why would the content-type cause issues? Can't really wrap my head around the issue...

1

There are 1 best solutions below

0
On BEST ANSWER

If you're accessing your "section" though custom means and not using the .Net configuration mechanisms, you might as well register the type as System.Configuration.IgnoreSection. This will make it easier to modify your configuration object without having to mirror the changes in the config file. You don't even need to derive your objects from any .Net Configuration objects.