Hi all I have seen many articles on url rewriting but I didn't find any as per my requirement. Assume I have two pages Default.aspx and Default1.aspx.. On initial load I would like to re write my Default.aspx to some thing like urlrewrite\dummy.aspx and on my Default.aspx I will have a button when I click on this I am going to redirect to Default1.aspx I would like to rewrite this to urlrewrite\dummy1.aspx
I just post the sample rewrites but if there is any better way of redirecting can you please help me..
Also what is the best way to rewrite all pages if I have some 20-50 pages
my global.asax file
<%# Application Language="C#" %>
<%# Import Namespace="System.Web" %>
<%# Import Namespace="System.Web.Routing" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
string root = Server.MapPath("~");
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(root);
System.IO.FileInfo[] files = info.GetFiles("*.aspx", System.IO.SearchOption.AllDirectories);
foreach (System.IO.FileInfo fi in files)
{
string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
routeCollection.MapPageRoute(fi.Name + "Route", fi.Name, pageName);
}
routeCollection.MapPageRoute("DummyRouteName1", "Dummy", "~/Default2.aspx");
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
You can add routes in your Global.asax file on application start:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("DummyRouteName", "Dummy", "~/Default.aspx");
....
}
Usage:
Response.Redirect("~/Dummy");
In url you will see: (server)/Dummy
Edit:
here is how to automatically add routes:
// Get root directory
string root = Server.MapPath("~");
DirectoryInfo info = new DirectoryInfo(root);
// Get all aspx files
FileInfo[] files = info.GetFiles("*.aspx", SearchOption.AllDirectories);
foreach (FileInfo fi in files)
{
// Get relative path
string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
// Add route
routes.MapPageRoute(fi.Name + "Route", fi.Name.Replace(".aspx", ""), pageName);
}
I assume that you got that rewriting part covered and only problem is postback, you can set postback to "friendly" URL by seting form action, like this :
Page.Form.Action = Page.Request.RawUrl;
Related
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
static void RegisterRoutes(RouteCollection routes)
{
//routes.MapPageRoute("Uniquename", "Name to shown on Adddress bar AND for redirecting", "Physical Path to the page");
routes.MapPageRoute("Home", "StoreFrontPage", "~/BestSeller.aspx");
routes.MapPageRoute("index", "MainPage", "~/index.aspx");
routes.MapPageRoute("ProductDetails", "DetailOfProduct", "~/ProductDetails.aspx");
}
Above codes helps me change the name of the url when i use "href" or "response.redirect" so when i open the first webpage (the starting page) i could not hide the url.
i do not want users to see the name of the webpage.aspx file.
thanks for any advices/help!
I want to use file upload control in dnn module programming.
I know there is DnnFilePicker in dnn, but I want a simple code that each user can upload a file and after that can display, edit and delete it.
There is this code but is not complete.
<%# Register TagPrefix="dnn" Assembly="DotNetNuke.Web" Namespace="DotNetNuke.Web.UI.WebControls" %>
<dnn:DnnFilePicker runat="server" ShowFolders="false" ID="fpUserFiles" FileFilter="pdf,gif,jpg" />
In Page_Load event, set the folder:
// Limit filepath to user's folder
fpUserFiles.FilePath = FolderManager.Instance.GetUserFolder(User).FolderPath;
what should i do?
If you have some sort of Save event on your module view, you simply get the FileId of the file that was selected by the user to save it in the database:
protected void btnSubmit_Click(object sender, EventArgs e)
{
ModelData model = new ModelData {
FileId = fpUserFiles.FileID
};
// TODO: Save your model data
}
In your Page_Load event, you can also load model data (if user is editing an existing model) by setting the fileID on the File Picker. That will pre-select the file in the picker control.
fpUserFiles.FileID = model.FileId
To use the fileId in another module view, you can get it from your model data and get the attributes of the file like this example:
FileInfo fi = (FileInfo)FileManager.Instance.GetFile(model.FileId);
if (fi != null)
{
pic.ImageUrl = "/" + _currentPortal.HomeDirectory + "/" + fi.RelativePath;
}
With this code my problem is resolved.
each user can upload its files in its folder.
protected void Page_Load(object sender, EventArgs e)
{
username = UserController.GetCurrentUserInfo().Username.ToString();
}
protected void Button1_Click1(object sender, System.EventArgs e)
{
var folder = Server.MapPath("~/uploads/Company/" + username);
if (this.FileUpload1.HasFile)
{
Directory.CreateDirectory(folder);
this.FileUpload1.SaveAs(folder + "/" + this.FileUpload1.FileName);
}
}
I have a simple global.asax file that runs some code at startup and stores a handle in a variable. I want to access that variable from my other files in the project. Here is my global.asax:
<%# Application Language="C#" %>
<script runat="server">
static JustTesting justTesting;
static public JustTesting JustTesting { get { return justTesting ; } }
void Application_Start(object sender, EventArgs e)
{
//my code here
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
</script>
And when I want to use that variable...
ASP.global_asax.JustTesting
...which works, but I'm sure there must be a more elegant way of calling it instead of having to add ASP.global_asax. all the time.
You can use Application object.
Reading:
var x = Application["x"];
Writing:
Application.Lock();
Application["x"] = "value";
Application.UnLock();
Reference: http://msdn.microsoft.com/en-us/library/94xkskdf(v=vs.90).aspx
You can also create your own class, which should be thread safe.
I am using a asyncfileupload control to upload a file there i am taking the path in a view state like this:
protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string name = System.IO.Path.GetFileName(e.FileName);
string dir = Server.MapPath("upload_eng/");
string path = Path.Combine(dir, name);
ViewState["path"] = path;
engcertfupld.SaveAs(path);
}
Now when i am trying to save that path in a buttonclick event i am not getting the value of viewstate:
protected void btnUpdate_Click(object sender, EventArgs e)
{
string filepath = ViewState["path"].ToString(); // GETTING NULL in filepath
}
In this filepath i am getting null actually i am getting error NULL REFERENCE EXCEPTION
What can I do now?
Put the Path value in the Session object instead of the ViewState, like this:
protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
....
string path = Path.Combine(dir, name);
Session["path"] = path;
}
Then in the Button Click:
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (Session["path"] != null)
{
string filepath = (string) Session["path"];
}
}
I guess the upload process is not a "real" postback, so the ViewState will not be refreshed client side and won't contain the path upon click on btnUpdate_Click
What you should do is use the OnClientUploadComplete client-side event to retrieve the uploaded file name, and store it in a HiddenField that will be posted on the server on btnUpdate_Click.
Here is a complete example where the uploaded file name is used to display an uploaded image without post-back :
http://www.aspsnippets.com/Articles/Display-image-after-upload-without-page-refresh-or-postback-using-ASP.Net-AsyncFileUpload-Control.aspx
I have added the following code to my Global.asax file:
<%# Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
etc.....
But my BeginRequest function just gets ignored. How do I redirect my entire application from http: to https:?
If you're using a master page or a base class, I would put your logic there. Global events shouldn't be relied upon for logic like this.
Put the logic in Page_Load (or earlier in the lifecycle) of the master page or base class like this:
protected void Page_Load(object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
You could do the above at another point in the lifecycle if you wanted too, like PreLoad or PreRender.
Using global events
If you're going to use a global event, I would actually use Application_EndRequest, because it gets called on every request so the application can clean up resources.