ASP.NET - Response redirect to different folder - asp.net

I am attempting to redirect a gridview on selection. However, I am getting stuck on redirection when the page I am trying to redirect to is in a different folder.
The gridview is in a folder named HR. I am trying to redirect this to a file called Staff within a folder called Staff (Staff\Staff). How can I redirect to a different folder?
If e.CommandName = "Select" Then
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("DetailsKey", GridView1.DataKeys(e.CommandArgument).Value.ToString)
Response.Redirect("staff\staff.aspx")
End If

Response.Redirect("~/staff/staff.aspx")

The main thing is to use / rather than \. You aren't redirecting to a folder on the server, but to a path on the website (the fact that this means a folder on your server is just an implementation detail).
You can do all the forms that you can with relative links. Hence "staff/staff.aspx" goes to the file called staff.aspx in the folder called staff that is in the current folder (assuming your folder-and-file based system). "../staff/staff.aspx" goes up a folder, then to staff then to staff.aspx. "../../staff/staff.aspx" goes up two first. "/staff/staff.aspx" goes to the root of the domain on (http://mysite.com/staff/staff.aspx, etc).
As well as all of these, "~/staff/staff.aspx" goes to the root of the application, then to staff within that, then to staff.aspx. This is useful if you work on the site such that this would be in http://localhost/currentProject/staff/staff.aspx because the project is at http://localhost/currentProject/ but deployed to http://mysite.com/staff/staff.aspx as the site is at http://mysite.com/. This way the same code works both ways.

This should do the trick
Response.Redirect("~/staff/staff.aspx");

Related

Custom Folder Structure in ASP.NET MVC 5

I'm trying to determine if it is possible (or practical) to implement Uncle Bob's Screaming Architecture in ASP.NET MVC 5 rather than using the default folder structure.
Here's a link to a description of the Screaming Architecture: http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-Architecture.html
A hypothetical folder structure would look something like this:
Root
Customers
Controllers
CustomerController.cs
Models
Customer.cs
Views
Index.cshtml
Details.cshtml
Update.cshtml
Employees
Controllers
EmployeesController.cs
Models
Employee.cs
Views
Index.cshtml
Details.cshtml
Update.cshtml
Shared
Views
_Layout.cshtml
Error.cshtml
_ViewStart.cshtml
Web.config
The corresponding URL routes would look like this:
http://www.example.com/customers/ => Customer Index
http://www.example.com/customers/details/1 => Customer Details
http://www.example.com/customers/update/1 => Customer Update
http://www.example.com/employees/ => Employee Index
http://www.example.com/employees/details/1 => Employee Details
http://www.example.com/employees/update/1 => Employee Update
I've created a custom RazorViewEngine and added the appropriate view location formats (e.g. "~/{1}/Views/{0}.cshtml") and partial view location formats (e.g. "~/Shared/Views/{0}.cshtml"). I've also moved the shared _ViewStart.cshtml to the root and merged the Views/Shared folder's web.config with the root-level web.config to avoid having to duplicate these two files in all of the View folders.
Everything works great, however, if I try navigating to an index page (e.g. http://www.example.com/employees/) I get a 403.14 Error (Forbidden). All other routes (including http://www.example.com/employees/index) work just fine.
My guess is that IIS is explicitly blocking the route to the controller's index method because the URL coincides with a folder in the filesystem and directory browsing is disabled by default. If I enable directory browsing, however, it actually takes me to the actual directory listing rather than routing to the controller's index method.
I can move the Customers and Employees folders into a subfolder (i.e. move them out of the root) and everything works fine, but I'd like to try to keep these folders at the top level (per the Screaming Architecture guidelines).
Does anyone have a solution for this issue?
Please note that MVC Areas is not the solution I'm looking for as it does not allow for the folder structure described above (i.e. top-level folders named after high-level use cases and views contained directly within the Views folder rather than in a subfolder).
I'm betting you are right about IIS then. If you have two paths mapped to the same resource, the physical path is checked first on the IIS side.
I was digging around the routes configuration and found the property RouteExistingFiles on RouteCollection and think this could work.
I set the value to true and tested locally with an empty folder in the project, a route redirecting to Home/Index, and navigating to localhost:xxx/MyFolder. It worked correctly.
So then all you should need to do is set this property to true for it to choose Asp.net routes first instead of physical routes.

ASP domain and files

I have no idea about asp, but I had to do some modifications in a web site, an easy modification. So I downloaded all files from server and I did all the modifications in Visual Studio 2013. Then I tested each page in the local host and it was perfect.
When I uploaded the files, I created a folder called "development", to tested it before I changed in the real site, so, my real site is for example "www.realsite.com" and my new folder is inside, with all the file, so I write in my url "www.realsite.com/development" and it shows the page, but not the one I had modified, but the real site. I want to know if there is a config file to change the path of the development site to see the changes I make and not the real site, because if I click in the development site a menu, it sends me to the page in the real site.
I hope you can help me with this,
Thank you!
PS: Do you know what is the meaning of "~/" in for example : src="~/folder/folder/xxxx.xx"
This is because the URLs in the project are using absolute paths, all pointing to the root. If they were using relative paths, moving the project to a folder and running it from there would work just file.
The difference:
... <-- absolute, note the leading slash
... <-- relative, no leading slash
Well, it depends on what kind of changes you are referring to, what kind of ASP.net site (or application).
The ~/ in ASP.Net means "path from application root". A subfolder (the new folder you created) in an existing application is just that, a folder. It is not "another application root". So if the existing code refers to "its root", e.g. where it uses ~/, it's probably not what you would expect..
Again, not enough info, but if you experience more unexpected behavior, it will probably be because of this (application scope).
Ref: ASP.NET Web Project Paths
ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application.
Hth...

ASP.NET sub domain website's with same codes but different in Contents & Images

I currently have a website for eg:- www.domain.com & i need to create 3-4 sub domains for this website like
abc.domain.com,xyz.domain.com ...
I want to maintain the same codes for all the subdomains, & for each of the Sub domains i have the same look & feel except the css style, the logo's, the Page title's & few Page contents would be changing.
The titles & images(i will only store the image name) can come from the resource file,
Like for example i will have Resource file for each sub domains & my website should automatically pick the resource file based on the domain.
but not sure which is the best & easy way to achieve this.
Similar Question I have Seen based on UI Culture. I donot want to follow this approach. As i will be using English for all my subdomains & in future i will be having more sub domains.
If you have full-control over your web server and you are using IIS, you can achieve this easily by having multiple sites pointing to the same physical location in the file system. Then, each site can (and must) have a unique binding which will allow IIS to route incoming request to the appropriate site (sub-domain). Note that it is important that you have each site running on its own app pool rather than sharing the same app pool
Furthermore, you need to consider that the scalability of this approach is very limited, if in the future you need to have...say, hundreds of sites it can potentially add a lot of overhead to your web server. So, if you need a scalable solution you should go for a multi-tenant approach instead which you can easily load-balance.
One last note, images bundled up in resource files is a pretty bad idea...avoid it!!!
Site-specific content
Having different sites pointing to the same physical location doesn't mean that they have to share the same content all the time. You can still decide what content to serve dynamically based on the request context by simply inspecting the request object...this goes for dynamic ASP.NET pages, however for static pages you will need to create a virtual directory for each site and point them a location specific to that site only...this will allow you to customize the content for a specific site...see screenshot below how I have a "login" virtual directory which points to somewhere else...
Example
This is a very simplistic example. Suppose you have two sites set up...
en.domain.com
es.domain.com
The sites are pointing to the same physical location...C:\inetpub\domain\site
Then, there's a page index.aspx that has a panel in it that displays a welcome message...
<asp:Panel ID="pnlWelcome" runat="server"></asp:Panel>
for the "en" sub-domain you want to display "Welcome to Our Site" in english but for the "es" sub-domain you want to display "Bienvenidos a Nuestro Sitio" in spanish.
Then, if you set up a virtual directory for each site pointing to a separate location in the file system....say for the "en" domain the location is...C:\sitescontent\en\ and for the "es" domain the location is ...C:\sitescontent\es\, you can then place a user control, xml file, whatever you need inside these site-specific folders with the site-specific content. Then it is as easy as referencing the site-specific content with a virtual path...
string path = MapPath("~/content/{your_site_specific_content}");
or
Control ctrl = LoadControl("~/content/{your_site_specific_content}");
pnlWelcome.Controls.Clear();
pnlWelcome.Controls.Add(ctrl);
If you have a more complex architecture in your application, you would want to inspect the Request object to find out in which context the request is executing whether in the "en" site context or "es" site context....
HttpContext.Current.Request.Url.Host;
By NO means, I'm suggesting this the best way to handle localized resources in your system...it is just an example of how to handle site-specific content.

Accessing folder outside the website directory in asp.net

I have around 20 websites created a single web-server, now i have made a tool which will be common for all the websites, so i have a common user-control, which i am using on .aspx file inside all the website
Now i want to keep these user-controls in a location outside the website directory so that i can access these same control to all the websites.However i cannot find a way to get the directory outside the website's root directory. I googled around and saw the option to make the directory as virtual one, so that it will be accessible to the site, but here i would have to link the folder to one website at a time, so it would be same keeping controls in separate websites, which i want to avoid for update reasons.Can anyone tell me how can i achieve this.
Hi In rough unchecked code something like:
You can access like this.
webRootPath = Server.MapPath("~")
docPath = Path.GetFullPath(Path.Combine(rootPath, "..\Documents/MyDocument.xml"))

Drupal Commerce Kickstart - Making Test site of Live site

I want to do developments on my client's website but by making a clone of it. So, main website url is: http://website.com and the clone i am trying to create is: http://test.website.com.
So far i've done the following:
copied entire root directory into public_html/test dir (with folders config,field,FirePHPCore,fontyourface,includes,js,misc,modules,scripts,sites,styles and themes)
created a subdomain in cPanel for test.website.com
checked the file settings.php (inside sites/default folder) for $base_url but found it commented, so left it as it is unchanged.
copied db via phpMyAdmin and updated the new db details in settings.php (inside sites/default folder).
inside the table variable, two rows with the name securepages_basepath and securepages_basepath_ssl. Changed their values from http://website.com to http://test.website.com (using the variable_get and variable_set functions).
Now i can access http://test.website.com but when i click on login (from header) it takes me to http://website.com/user and if manually type http://test.website.com/user and login then it takes me to http://website.com/users/admin then i have to manually type in correct address http://test.website.com/users/admin.
And when i logout, it again takes me back to http://website.com.
So i want to know how can i completely make it to work on http://test.website.com?
Are there more variables to change?
And how i can make 100% sure that the test site is only using test and not the live site. I am afraid of messing up live website.
Please advice, thanks!
I fixed it by disabling the secure pages from inside the mysql database. It was inside variable table and securepages_enable field. It was in blob so i had to download the blob first and opened it in notepad and changed the value inside it from 1 to 0 and then uploaded it back by updating the securepages_enable field.
I had to do this because after logging in from my test url, the urls were redirecting back to the live website, so whatever change i was making, it was all affecting the live site.
Hope this helps to someone with similar case. Thanks!

Resources