How do I set the default document to a subdirectory page? i.e. instead of default.htm something like /myWebApp/newDefault.htm?
The page I want is in a directory in wwwroot
Many thanks
Have you looked at URL Rewriting?
http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module
Or if you are using ASP.NET you could do something like this:
public override void ProcessRequest(HttpContext context) {
context.Response.Redirect("myWebApp/newDefault.htm", true);
}
-M
Related
I have few dinamically generated urls like http://localhost:35228/begineercontent?name=lanaguages&id=23, I used routing to hide .aspx extension and now i want to see above url like this http://localhost:35228/begineercontent/lanaguages/23 i tried few url rewrite methods from iis url rewrite tool nothing worked kindly please help me out
This can be done very easy. You need to change/add this code in your global.asax.
void registerroute(RouteCollection routes)
{
routes.MapPageRoute("begineercontent", "begineercontent/languages/{Id}", "~/begineercontent.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
registerroute(RouteTable.Routes);
}
You also need to include the right assembly in global.asax by adding
using System.Web.Routing
In your begineercontent.aspx you need to add this code to the page load function.
var Id=( Page.RouteData.Values["Id"] as string);
A more detailled tutorial on how to work with routes can be found here:
http://msdn.microsoft.com/en-us/library/vstudio/cc668177%28v=vs.100%29.aspx
For instance, this url is http://stackoverflow.com/questions/ask, instead of something like http://stackoverflow.com/questions/ask.aspx. How can I do this?
You can use this tricks and tools:
http://forums.iis.net/t/1150350.aspx - IIS Solution
http://forums.asp.net/t/979581.aspx - ASP.NET 404 Page solution
Add a reference to the assembly System.Web.Routing and then add the following to your global.asax file:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"questions/ask/{question}",
"~/questions/ask.aspx");
}
If you're question is not specific to aspx pages you can do this with a .htaccess file on any webserver running apache (and mod_rewrite - but that's almost all of them).
See various questions on this site about .htaccess or check out many simple tutorials online.
Basically you can put a special file .htaccess in the root of the directory you wish to be affected, that essentially say
Instead of about_us/details.php you can just use about_us/details
This is a very simple example
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^questions/ask ../questions/ask.aspx [L]
This happens when the web server implements a handler that maps a given URL pattern to actual content to serve up.
In this site, it's probably from the use of the MVC framework (guessing).
On windows IIS you can use http://www.iis.net/download/URLRewrite
Application uses Forms authentication and has folders with excel files. Need to prevent
unauthorized, automated scripts or bots from accessing these folders. What would be best option to prevent this?
Have the authorization node of the lockElementsattribute value set in the
Web.config file.
Have a element added to the element in the Web.config
file.
3 Use (CAPTCHA) image control on each page of the application.
4 Use Robots.txt file implemented in the root directory of the application.
Have the Excel files mapped to the ASP.NET ISAPI filter.
Or are there better options? Httmodules?
You could protect the excel files by writing a custom httpmodule and validating that they are authed via the forms auth before giving them access to the file.
In addition I would use the robots.txt file as well to exclude them. Those that follow the rules will stop looking at that point. The rest will be taken care of with the custom httpmodule.
The most secure solution would be to store your files outside of the web-tree and then serve them up via a HttpHandler. The simplest handler to create in ASP.NET would be an .ashx Handler as outlined in this blog post.
Download File
Your handler would then check the user request to ensure the user is authenticated and then stream the file back. In simplified pseudo-C# code this would be something like:
public void ProcessRequest(HttpContext context)
{
string file = context.Request["file"];
if (user.IsAuthenticated())
{
OutputFile(file, context)
}
}
private void OutputFile(string file, HttpContext context)
{
string fileContent = LoadFileFromSecureDirectory(file);
Response.Output(fileContent);
}
Map the Excel files to the ASP.NET ISAPI filter and add a
<deny> element to the <authorization> element in web.config,
I have a virtual directory under my MVC website in IIS called "Files". This directory is at the same level as my Views directory. When I link to a file from my MVC app to a file under my Files directory, I get the following error:
The controller for path
'/Files/Images/1c7f7eb8-5d66-4bca-a73a-4ba6340a7805.JPG'
was not found or does not implement
IController.
It thinks that my Files VD is a controller. How do I access my files like a normal VD without MVC interfering?
Thanks.
When registering routes, try to add the following Ignore rules.
public static void RegisterRoutes(RouteCollection routes)
{
/* Ignore static content, see
http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx
*/
routes.RouteExistingFiles = false;
routes.IgnoreRoute("Content/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("Styles/{*pathInfo}");
routes.IgnoreRoute("{*favicon}",
new { favicon = #"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });
//Ignore handlers and resources
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// your routes go here
}
ASP.Net looks for the directory first and then tries to match a controller, so what you are doing should work. Are you sure the file with that name exists and is accessible?
I think you'll have to add a call to routes.Ignore() a static route in your Global.asax file so that .NET MVC knows to ignore the request:
RouteCollection.Ignore(String) - MSDN
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"