How to dynamically serve static files? - c#

I'm building a ASP.Net Core project which will be used as an online-help for my app and it will serve static files which will display in-app contextual help.
However, one requirement is that each customer can have his own customized help. If he doesn't it should fall back to the default help page.
So I create a WebApplication project as a start. I would need to create a Controller with just one action that receives 2 parameters (customerId, helpId).
Where should the static files be kept (under wwwroot or Pages)?

Related

ASP.NET MVC reuse code between different website

I have two asp.net mvc website, they have the same page to update account data.
How to separate the repeated code(controller, *.cshtml, *.js, *.css... etc) for better maintainability? I don't want to modify code from one website and copy it to the other.
I have tried creating new website project only contains account pages, but the static files(*.js, *.css) can't be reused in this way.

Does my file-parsing logic go in my file handler or code-behind in ASP.NET webform?

I'm new to ASP.NET and am working on a file parser and was wondering what the structure of my project should be. So far I have:
WebForm.aspx (where my html/css/js goes. an upload button calls the filehandler)
WebForm.aspx.cs (the code-behind. it's completely empty right now)
FileUpload.ashx.cs (one class that posts the selected file to my server)
FileParser.cs (classes that I have written/will write in C# already that will read in a file and parse it the way I like and return another file with the analytics and save to the user's computer)
I'm wondering where the FileParser.cs files should go. I need to be able to read the contents into an object that will parse the file to do some analytics and then give the user a "Save to computer" button on the next page to save the analytics file. I'm guessing I also need another handler for that?
The fileparser.cs must go to the App_Code folder, or you can create a new dll library and compile it, then place it to Bin directory of your asp.net
Then the classes inside the fileparser.cs can be accessed by the code behind.
More to read:
ASP.NET Web Project Folder Structure
Shared Code Folders in ASP.NET Web Site Projects
Codebehind and Compilation in ASP.NET 2.0

POST to a .JSON static file in MVC Project

I've been developing a web project and using .json files to retrieve data via $.ajax calls on the client. Those calls all use POST, because parameters will be passed, etc.
When I put the .json files in the application, I receive a 405 error, complaining that I cannot POST to that file. I assume it wants me to use GET, but I want to use POST so that I don't have to change the way those methods are written when we move them to Controller Action calls, or Web Api, etc.
How can I allow a POST to a static .json file in an MVC project?
Any help is most appreciated.
The reason you can not POST to those files is because you don't have any handlers (PHP, ASPNET, etc) associated with .json files that can handle the POST verb.
A really easy work-around is to make a JsonController controller with actions corresponding to your views..
VB - you can translate
<ActionName("AJsonFile.json")>
Public Function AJsonFile As JsonResult
' Return the .json file content
End Function
That way you can keep your views as .cshtml files, which are registered with the asp net processor, and not have to worry about mapping a new file type.

Is there a way to have IIS default document (default.asp) take precedence over .Net routing?

I am trying to migrate a site from classic ASP to .Net (using WebMatrix). I am planning on mixing the new .cshmtl pages in with the legacy .asp files and slowly migrate the site over time. To date, I have individual .asp pages for each piece of content (each url). As I move to .Net, the content will be coming from a database, so there will not be specific pages for each url. I love the power of routing that is available in .Net and it works well and plays nicely with the classic ASP with one exception so far.
My example is that I have an ASP page located at /crafts/default.asp and currently reference that everywhere as just /crafts/. The IIS default document setting takes care of serving the default.asp page when i just reference /crafts/. I want to start to post new items into this area of the site but run as .Net pages. so I created a /crafts.cshtml page which is designed to handle different urls and go to the database and lookup the item and display the info. So as an example I have legacy page at /crafts/fall-crafts.asp and this works fine - because there is an .asp page sitting there named that. But when i reference /crafts/ .Net takes over and serves the /crafts.cshtml page instead of serving the /crafts/default.asp file. Is there something I can do to keep the power of .Net routing in place, but still have IIS serve the default document in this case?
Modify your RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("crafts/");
...
See RouteCollection.Ignore for documentation.
Similar question (mentions some other possibly needed workarounds): Ignore folder in ASP .NET MVC
The default "routing" built into the Web Pages framework relies on matching incoming URLs to physical files on disk, looking first for the existence of cshtml or vbhtml files. All the time you have a crafts.cshtml file at the same level as a folder called crafts, the file will be served first. The solution is to remove or rename you crafts.cshtml file, but currently that will mess up your URLs.
There is a package which offers greater management over routing in Web Pages applications. It's called Routing For WebPages. It allows you to route URLs to any file name you like. You can download that via the Package Manager or Nuget and read this article that I put together that explains how to use it: http://www.mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages.

How to dynamically build multiple sites using MVC

is it possible in MVC to setup a site where it would have one set of controllers but dynamically build the site based on a url that looked like this:
www.a.com/sitename/index
Each sitename would have it's own configuration, data, and look and feel.
Well, you could simply deploy the same MVC (or any) web application to separate virtual directories, each with their own configuration (and CSS files, etc.).
Alternatively, you could change the default route table to include {tenant} (which is what this is often called) as part of the path, and pick that up in your controllers.

Categories

Resources