Allowing file extensions in MVC3 - asp.net

Hello fellow programmers
I am sitting trying to do something with MVC3. Just exploring the library. Not doing anything fancy. I have created a project and a controller. Now I am trying to let MVC3 to allow me to do fx http://localhost/MyController.json, http://localhost/MyController.xml or just http://localhost/MyController. In the .json and .xml examples I get errors. The third is of course allowed. I have tried to google this, but I do not get anything that helps me.
Can anyone here tell me how to allow file extension in MVC3?
Thanks.
EDIT
I have not made any changes to the IIS like mapped file extensions.

You should add a route that includes an extension:
routes.MapRoute("ExtensionRoute",
"/{controller}.{extension}",
new { action="Index", extension = UrlParameter.Optional
);
This example maps the extension to a parameter in the action.
You can also make a route with a hard-coded extension.

Related

Razor Project on with CSHTML files

A project kind of landed on my hands and I am having issues with one thing.
I have an already working project that is installed on a local server. I am able to make changes to existing pages, but I just tried creating a new page from copying an existing page and modifying it, but I keep getting an error when I try to open it. This is the error:
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.
From my research so far, it looks like there has to be a controller in place to properly map the new pages, but I have not been able to find that within the project. I copied the project and opened it with ms visual on a different machine, same issue, everything opens and works just fine except the new page that I added. While the project is open on ms visual I do not see a folder for controllers either.
I would greatly appreciated if someone can point me on the right direction
Thank you,
Cesar R
With Model/View/Controller, you need a Controller to match with the View.
Let's say you add a new folder and cshtml to your file in the following folder:
~/Views/Test/Test.cshtml
You also need a controller named similarly in the following folder:
~/Controllers/TestController.cs
and inside that controller you need a method that matches the name of the view:
public ActionResult Test()
{
return View();
}
That will make http://[yourwebsiteurl]/Test/Test be an active page.

Create pages in published code

I want to know whether we can add new pages in depolyed MVC project code?
Based on my experience, it is not possible.
Please share your thoughts.
P.S: I need to add two new pages in published MVC code. So there is no chance to write any additional code in controller and model part.
Yes, you can add .cshtml to your publish folder in view, but you cann't add server side code in publish dll

Cannot route static files in ASP.NET WebForms

We have legacy code to maintain and, to solve a specific customer customization problem, we want to route calls to some files to other files. That is, when the app calls a particular ASPX, it will end up hitting another ASPX.
If you call:
www.foo.com/admin/admin.aspx
It will actually hit:
www.foo.com/customizations/customer1/admin/admin.aspx
This is not a good design but this is legacy code. We just want to solve this.
We are using the System.Web.Routing framework to solve it. This works fine when you set RouteExistingFiles to true, except for static files (CSS, JavaScript and Images).
When I first tried it, it retrieved this error:
There is no build provider register for the extension '.css'.
So I did register a build provider in the web.config file for the .css extension. I used this build provider: PageBuilderProvider because someone recommended it in the internet.
It works! But the CSS is being served with text\html content type.
How do I achieve this?
TL;DR: I want to use routes in ASP.NET Web Forms to make a call for a specific CSS file to actually retrieve another one. A customer needs this for customization.
Try coding a HttpHandler. I had to do something similar but for PDF files, I coded a custom HttpHandler in the end - works very well. You can even set the content type in the HttpHandler code and have a pattern matched path the handler will be used for in the web.config. You can also configure it in web.config not to execute if the path does not point to an existing file e.g. so a 404 is returned without having to code that in the handler itself. I can't post my code (VB.NET) ATM because I'm using a tablet but google search for tutorials. You will also probably need to use the TransmitFile function to actually write out the css file. Is it a web forms project or web site? If its a web site there is a special way of registering the HttpHandler in the web.config.

How do I download an msi file via an asp.net button?

So, I've created my wonderful winforms app that I want to unleash upon the world, and now I am trying to create a simple website to host some basic information and link to the setup file (msi installer file )....
I have a button on the asp.net page and the setup file setupApp.msi in same folder as the asp.net page. I am currently trying the following:
Response.Redirect("http://./SetupApp.msi");
But this best guess at what to do is not working. Is there something wrong with Mime types here? What do I need to put in the click event to allow users to download this file?
The path you are passing in to the method is not valid (there's no server name called ".").
You can pass in a relative path and it should work fine because ASP.NET will resolve the path:
Response.Redirect("SetupApp.msi")
Or if it's not in the same folder, try one of these:
Response.Redirect("../Downloads/SetupApp.msi")
Response.Redirect("~/SomeFolder/SetupApp.msi")
Keep in mind that you don't necessarily have to do the whole redirect at all. Instead of writing code in an ASPX file you could just have a link to your MSI:
Download my app!

Using Routing in Asp.Net Webform Application

I am using System.Web.Routing in the Asp.Net Webform Application. I wrote the following route in the global.asax
routes.RouteExistingFiles = true; // I made true/false both, but none works
routes.Add("competition", new Route
(
"Test",
new CustomRouteHandler("~/Test/WebForm1.aspx")
));
And the directory structure is the following:-
Application
|
|--- Test (Folder)
|--- Webform1.aspx
When I write in the browser http://localhost:xxxx/Test/ (using Casini), the request is handle in the traditional manner not through the routes, and, it gives me the "Directory Listing -- /test/" page.
Could you please help me out?
I had the same problem, and I chose the pragmatic solution that the file that should handle the default path is called Default.aspx
routes.Add("competition", new Route ( "Test", new CustomRouteHandler("~/Test/Default.aspx") ));
Are you using Routing straight out the box for WebForms, I have just implemented this for "WebForms" specifically, since there is some things to be aware of:
http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx
HTH
Update:
Using the implementation in the link still produces the same error, since the actual folder exists.
I would think that since the folder exists, it would be served (in some cases listing the folder contents may be desired)
Maybe taking a different approach would be better for instance, if the pages all point to specific extranet login pages maybe an extra descriptive folder would work, eg: /Extranets/Test/ ?

Resources