url rewrite in asp.net for dynamially generated urls - asp.net

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

Related

Embedding resource in DNN module gives 404

I'm trying to embed a javascript file in a DNN module, the script tag is created but the content is a 404 error.
The namespace of my project is "Carroussel" the location of my file is "js/jquery-carouFredSel-6.2.1.js"
I've added the following code to AssemblyInfo.cs
[assembly: WebResource("Carroussel.js.jquery-carouFredSel-6.2.1.js", "application/javascript", PerformSubstitution = true)]
And the following to the OnInit of the module:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.ClientScript.RegisterClientScriptResource(GetType(), "Carroussel.js.jquery-carouFredSel-6.2.1.js");
}
The scripttag is created in my html:
<script src="/WebResource.axd?d=gnpD_kHZKrDN3DKUCn2faE-x6tdus2cDJ1HjymfAToCQgqX2ggJno51OH2VqPx8ArZPoUm5RuWxhg8uAOTZaysKJE7jz3kpB6gHWbD25o2plphslAJao3Rs5ybJz_M9vLB1NmgPujgOBCt3pDGs9aY_lTy04oPXMqX6dyz0AHhjrbZlKl8muO0ZRGnAiWwdicmeAlg2&t=635349640675857733" type="text/javascript"></script>
But as soon as I view the source i get:
The resource cannot be found.
Description: 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.
Requested URL: /WebResource.axd
Any ideas on what to change to make this working?
I would recommend that you not embed it, but let DNN handle the registration/loading of the resource. Doing so will allow you to easily see references to the JS if you have CDF disabled in DNN, and mask the references and have the content minified with CDF enabled in DNN.
Here is how to reference the client resource manager in DNN.
override protected void OnInit(EventArgs e)
{
DotNetNuke.Framework.jQuery.RequestUIRegistration();
ClientResourceManager.RegisterScript(Parent.Page, "~/Resources/Shared/scripts/knockout.js");
ClientResourceManager.RegisterScript(Parent.Page, "~/desktopmodules/DnnChat/scripts/moment.min.js");
ClientResourceManager.RegisterScript(Parent.Page, "~/desktopmodules/DnnChat/scripts/DnnChat.js",150);
base.OnInit(e);
}
pulled from https://github.com/ChrisHammond/dnnCHAT/blob/master/View.ascx.cs

ASP.NET Routing in Global.asax

I'm trying to add a route in my web forms application by following this:
http://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_routes_to_a_web_forms_application
I've added the route in my Global.asax file like so:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "/WebsiteName/{combinedPin}", "~/Default.aspx");
}
I then try to visit my website locally like this:
http:// localhost:12345/WebsiteName/test36u
But I get a resource cannot be found message so I don't think my route is correct. Can anybody see a problem with my code?
Any pointers would be much appreciated.
Thanks
You do not need to specify the name of your website as part of the route, try with this code:
routes.MapPageRoute("", "{combinedPin}", "~/Default.aspx");
With the above code, your link would look like:
http://localhost:12345/WebsiteName/test36u
If however your intention is that your users access your site using a segment named: WebsiteName then use:
routes.MapPageRoute("", "WebsiteName/{combinedPin}", "~/Default.aspx");
But in the precedent code your users will have to access your resource as follows: (probably not the expected result though)
http://localhost:12345/WebsiteName/WebsiteName/test36u

How to open folder from web page

I want to open folder from web page on clicking on a button.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("file://C://inetpub//wwwroot//myproject");
}
Please suggest me proper code in C# for open myproject folder.
This can be ONLY done in IE, no other browser has access to file system. You can assign the path to href of an anchor tag, and it should work.
Open
If you're looking at adding functionality to your website so that a user can upload files to it then look at this project on MSDN. It'll show you how to do this with an example ASP.NET project.
Maybe this statement can help you:
System.Diagnostics.Process.Start(#"C:\");

How to get rid of actual file-name in URL?

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

using dll extention instead of the standard aspx

I am replacing an existing web application, that all it's requests go through a url:
www.something.com/scripts/xxx.dll?args
I created my own aspx page that handles these requests and it is called:
www.something.com/scripts/xxx.aspx?args
My problem is that there are many existing links, from other website that refer to the xxx.dll?args url.
Can I create my own dll in .net that will receive the xxx.dll?args requests and process them?
This isn't a simple redirect, because I also need the args
I'd suggest to rather use Url Rewriting
After some more investigation I did the following.
Change the web.config, to make sure all requests go through my code by adding the following code:
...<system.webServer>
<modules runAllManagedModulesForAllRequests="true">...
Added a global.asax file to the web project, and within it wrote the following code:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Path.EndsWith("xxx.dll",
StringComparison.InvariantCultureIgnoreCase))
Context.RewritePath("/scripts/xxx.aspx");
}

Resources