asp.net hidden redirect with direction url - c#

How to do it:
When you access a certain page (like a folder), you can view information specific user, for example:
www.page.com/user001
Opens the default page www.page.com/userinfo.aspx in which that user get "user001" and display certain information. And the user see www.page.com/user001
I can do this with asp.net or IIS7?
something like subdomains

You can do this by adding the URL Rewrite Module to IIS. Check this out
http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module

You can store the URL Rewrite rules in the web.config file. For example:
<configuration>
<system.webServer>
<rewrite>
<rules>
****(Your URL Rewrite Rules)****
</rules>
</rewrite>
</system.webServer>
</configuration>
But if it's not possible for you to store it in web.config due to security or maybe for some performance issues, then you can store the URL Rewrite Rules in IIS.
I hope it helps.

you can use routing in global.asax file at application_start event instead of rewrite the url also this will redirect every this to the profile page

Related

MVC Web Application project under MVC Controller

I have 2 distincts MVC Web Applications accessed from:
1) product.main-brand.com (solution landing page)
2) admin.product.main-brand.com (solution admin landing page)
The product is going to change the location from:
product.main-brand.com to www.main-brand-com/product
And the admin has to change from:
admin.product.main-brand.com to www.main-brand-com/product/admin
I can't create a Virtual Directory for the admin because www.main-brand-com/product is a controller.
For example, NopCommerce does that, we can go from www.shop.com to www.shop.com/admin and it's changing project and not controller/action. How does he do that?
Here are some steps to achieve a similar solution like in nopCommerce 3.9
Create your primary Web Project
Foo.Web
Properties
AssemblyInfo.cs
Foo.Web.csproj
Global.asax
Global.asax.cs
...
Create your admin Web Project inside of your primary Web project
Foo.Web
Administration
Properties
AssemblyInfo.cs
Foo.Admin.csproj
...
Administration folder must be created using "Add project wizard" or using explorer. Do not create that folder using solution explorer.
Remove Global.asax from admin project
You don't need this
Add an AreaRegistration implementation to your admin project
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName => "Admin";
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute
(
name: "AdminDefault",
url: "admin/{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", area = "admin", id = ""},
namespaces: new[] {"Foo.Admin.Controllers"}
);
}
}
Modify Global.asax.cs to register areas
Add this to your project. Make sure it's called before your default routes.
AreaRegistration.RegisterAllAreas();
With this, all your controllers inside of Foo.Admin are found by
~/Admin/{controller}/{action} and all your controllers inside Foo.Web
are found by ~/{controller}/{action}
Since you have 2 distinct MVC web applications I see 2 possible solutions...
a) create an Admin folder inside your main web application and set it as the root of your admin web application i.e. put your whole admin website into the admin folder. I expect this will require some tweaking of web.config of both apps so they can coexist.
b) leave each web application in it's own IIS virtual Application then add rewrite rules in the main web application for the /admin path to the true IIS path and you may need some rewrite rules in the admin app back to the main app if there is expected to be a navigation path to it.
I would recommend that you do look into attribute routing.
https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
This allows you to do this:
[Route("/product/admin")]
That attribute decorator goes on your controller method after you setup attribute routing.
Hope that helps.
This can easily be achieved in asp.net mvc application by using the feature called Area (for detail see https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas).
Your main application will work for www.main-brand-com. You can add an Area for Product to be referred by www.main-brand-com/product and another Area called Admin to be referred by www.main-brand-com/admin.
You can add an Area in asp.net mvc application by right clicking on web application project then selecting Add from the quick menu and then select Area. It will ask for name of Area to be created. After giving name and clicking Ok it will create an Area with that name in Areas folder in your web application project. The newly created Area will have same folder structure like main application like Controller, Model, View etc. folders. It will also create a class (AreaRegistration.cs) for registering the routing details for this Area. The content of this class may look like below code.
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
As a rule, if you're working with an externally maintained platform, you should aim to make as few code changes as possible. You will save yourself the work of merging, testing and deploying your code changes when there's an update. This is especially important if the update is a critical one.
In this particular case, it is best to let IIS to reroute your URLs. It has no code impact and offers a major benefit. Visitors and search using deprecated urls are automatically redirected to newer ones. This is especially important for search engines. If they encounter a 404 error, they will often remove the urls and even the entire site. In other words, VocĂȘ pode perder muitas vendas!
You can reroute your urls in the IIS Manager using the Url Rewrite Module or through web.config files. I recommend web.config files since it can be uploaded and kept together with your project.
For your existing product subdomain site, add the following to the system.webServer section of the site's web.config file:
product.main-brand.com to www.main-brand-com/product
<system.webServer>
<rewrite>
<rules>
<rule name="product.main-brand.com Redirect" stopProcessing="false">
<match url="^\/?$" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*product\.main-brand\.com.*" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://www.main-brand-com/product/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
For your existing admin subdomain site, add the following to the system.webServer section of the site's web.config file:
admin.product.main-brand.com to www.main-brand-com/product/admin
<system.webServer>
<rewrite>
<rules>
<rule name="admin.main-brand.com Redirect" stopProcessing="false">
<match url="^\/?$" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*admin\.product\.main-brand\.com.*" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://www.main-brand-com/product/admin/{R:0}" />
</rule>
</rules>
</rewrite>
These addition will redirect users to the new URLs. Refer to the following article for how to setup IIS url rewrite for further information:
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-global-and-distributed-rewrite-rules
Changing the redirection in IIS is one part, but your application needs to understand how to route the new url scheme internally. Without your app's specific routing code, I can't be of much help. If your application, however, is using subdomain's url parameter and you want to change it, refer to the following article:
https://benjii.me/2015/02/subdomain-routing-in-asp-net-mvc/

how to redirect url request to https in webforms proj

Any thoughts on why implementing Application_BeginRequest to check for the Url scheme (to replace http with https within the requested url and redirect) coupled with
<modules runAllManagedModulesForAllRequests="true">
in the config file of a webforms solution is not enough to achieve this purpose?
So far it works only for urls like: www.mysite.com/path1, not for www.mysite.com/path1/file.aspx. here it says it didn't find the file. How can i force it to switch to https before actually looking for a file?
You can do redirects on global.asax file on Application_BeginRequest()
This is one of the ways you can go about it.

DNN RewriterConfig redirection Rule

I try to redirect www.myDNNSite.com/hastinfo/[...] to http://172.16.244.43:83/[...] in DNN 7.x
To do that, I added a rule in SiteUrls.config as this blog showed https://bertcraven.wordpress.com/2008/05/21/quick-n-dirty-redirects-in-dotnetnuke/
<RewriterRule>
<LookFor>[^?]*/hastinfo/(.*)</LookFor>
<SendTo>http://172.16.244.43:83/$1</SendTo>
</RewriterRule>
This not work if there is a . after hastinfo, so:
www.myDNNSite.com/hastinfo/test -> OK 172.16.244.43:83/test
www.myDNNSite.com/hastinfo/myhandler.axd -> KO error 404 and no redirection
It seems that DNN make something with URL if there is a . founded.
How to make my redirection working even if a . (dot) is present in url ?
EDIT:
My objective with this redirection is to have the same end point for my dev and production environment. I have three server on the front end and the Web Service behind the www.myDNNSite.com/hastinfo point to three differents servers to load balance the charge.
Before update to DNN 7.X, I was using ManagedFusionRewriter that make the job.
But It is not updated from 2009 and now with IIS 8.5 I look a better way to make this redirection from a config file.
EDIT 2:
I try to add IIS Mod Rewrite in my IIS 8.5 and write in Web.config:
<rewrite>
<rules>
<rule name="Imported Rule 101" stopProcessing="true">
<match url="^hastinfo/(.*)" />
<action type="Rewrite" url="http://172.16.244.43:83/{R:1}" appendQueryString="true" />
</rule>
Without success ... here the Failed Tracing:
Using IIS Redirects in combination with the DNN Friendly Url providers is often a recipe for disaster sadly.
In this case, the reason that you are having issues more than likely is that the .axd is being captured by the ASP.NET process and that is preventing the re-writing from happening.
Based on what you are looking to do, can you explain a bit the true goal? The reason for this answer rather than a "do this" is that redirecting with DNN to a non-standard port is also going to cause issues as there is a setting for "usePortNumber" in the web.config that you will want to have on if using a non-standard port.
I finally found a way to make it works.
Thanks to: http://blogs.msdn.com/b/asiatech/archive/2011/08/25/return-404-4-not-found-when-url-rewrite.aspx
You need to install Application Request Routing and enable Proxy, then it will work with URL rewriting to remote servers (regardless where or what they are) since the Routing will take care of that.
http://www.iis.net/download/ApplicationRequestRouting

respond to .html extensions

I am moving a website from a static website to MVC 5. I need to create controllers/actions to respond to requests with old URLs. ( and return a redirect, page moved ) If I just hardcode the old URL in attribute routing, I get
HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable
What is the best way of approaching this. ( what I mean is that how could I make a route for a url with .html extension, and ASP.net respond to it)
I think right now IIS sees that .html and tries to send the file to the client without going through the application. How could I remove that behavior on a shared hosting ?
the html files that are being served right now are in different folders.
example:
I need to map this URL to a controller/action:
example.com/services/Renovations.html
we will be mapping it to controller => services , and action => renovations
example.com/contactus.html
we will be mapping it to controller => contact , and action => index
Write your application using MVC Controllers/Actions as appropriate for your new architecture, then implement a set of URL Rewrite rules at the IIS level to map the old URL's to new ones. The Rewrite rules will be stored inside your web.config file which is handy for moving the configuration from Dev to Staging to Live environments.
If you give some examples of your old URL's and their new corresponding routes, I could give you some sample IIS Rewrite rules to match. This works best if your old URL's followed a pattern, e.g. /products/{skucode}.html etc
EDIT: Some sample rewrite rules to match the requested redirects.
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Static rewrites" enabled="true" stopProcessing="true" >
<match url="^(.*?\.html)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{static site rewrites:{R:1}}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="Static site rewrites" ignoreCase="true">
<add key="contact.html" value="contact" />
<add key="services/renovations.html" value="services/renovations" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>
It should be clear how to add more keys to the rewriteMap for other pages you'd like redirected. To clarify, these are Redirects and will return HTTP 301 Moved Permanently response codes to the browser and transparently redirect it to the new route.

Disable Caching For A Single JS File Using Web.config

I currently cache everything possible on my site (images, JS, CSS). There is only one JS file that I need to be loaded fresh every single time. How do I omit just one file from caching, using web.config, whilst leaving everything else cached?
Note that I tried another link here, and it didn't seem to stop the caching of my file:
How do I disable caching of an individual file in IIS 7 using weserver config settings
How about:
<configuration>
<location path="path/to/the/file.js">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</location>
</configuration>
(Note that the path is relative to the web.config file)
I don't think you can do it using web.config, but you could add a unique querystring parameter to the javascript url in order for it to be loaded every time:
If you are using ASP.NET
<script src="mycode.js?<%=System.Guid.NewGuid.ToString()%>"></script>
Set the path for it not as a static URL but get an ASPX page to serve the script. Inside your ASPX page just send back the text:
byte[] javascriptTextBuffer = GetMyJavascript();
Response.ContentType = "text/javascript";
Response.Write(javascriptTextBuffer);
Inside the page turn off caching.
Having said that, it seems to me that you are doing something wrong that have to load the JavaScript file everytime. Make scripts static but use parameters to drive versatility.

Categories

Resources