Vanity URLs with IIS URL Rewrite - iis

I feel like this should be easy, but I'm struggling. I'd like a user to be able to go to this url:
http://www.mysite.com/folder/some-id-text
and have URL Rewrite direct that request here:
http://www.mysite.com/folder/index.aspx?id=some-id-text
http://www.mysite.com/folder/some-id-text should be the only url the user ever sees.

in your project edit Global.asax file and add the code below.
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.MapPageRoute("somename",
"folder/{text-id}",
"~/index.aspx");
}
then in your index.aspx you can find this variable as
string text_id = RouteData.Values["text-id"].ToString();
further ref http://code.msdn.microsoft.com/Easy-Steps-to-URL-2f792901

Related

How to insert code in the static files like .html, .htm, .asp on IIS for non-asp project

I want to add a script on my IIS Server.
So that it will be applied on all the websites that are upload will have that script in their request response.
Anyone who knows how to do it?
I had implemented the IHttpModule and IHttpHandler, it works fine for the asp.net projects.
but if the website contains only html, css, and js files in the folder, this solution doesn't work.
Here the HttpModule and HttpHandler
public class MyCustomHttpModuleClass : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += OnPostRequestHandlerExecute;
}
public void OnPostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}
public class MyHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("<h1>alert('HELLO')</h1>");
}
}
I'm not sure if you have learnt how to add Custom Module and Handler in IIS. After tested your module and handler with static website, it works fine.
I will just give you a sample of adding them to IIS.
1.Create a project "class library .netframework". I name the project"ClassLibrary1"
2.Add class "MyCustomHttpModuleClass" and "MyHandler" to the project
3.Build this solution and find "ClassLibrary1.dll" in the "project/bin/debug" folder.
4.Copy "ClassLibrary1.dll" to the website root "BIN" folder.
5.Add managed module and handler by choose your dll.(should in the list after you copied)Just mention that your custom handler only work on the file extension you set up.
Now they work.

Is it possible to rewrite URL on the Server instead of the Client using the IIS Rewrite-Module

As I've described in my Title, I want to rewrite a certain URL not on the client-side who calls the URL but on the Server, so the request gets received by the locally (as Windows Service) running REST API.
Is there any possibility to do so?
We can use RewritePath method of HttpContext.
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.ServerVariables["SCRIPT_NAME"] == "/Home")
{
Request.RequestContext.HttpContext.RewritePath("/Index.aspx");
}
}
}

How to redirect any url that have hyphens to home url in asp.net mvc 5

I am creating a new site for one of my client. Their old site was developed using wordpress and hence it has 100s of broken urls like below:
http://www.example.com/best-gym-in-town/
http://www.example.com/video-gallery/
http://www.example.com/fun-stuff/
http://www.example.com/are-you-a-diabetes-patient/
http://www.example.com/john-in-media/
http://www.example.com/photo-gallery/
http://www.example.com/nutrition-program-that-suits-your-lifestyl/
http://www.example.com/our-range-of-fitness-tests/
http://www.example.com/corporate-group-workshops/some-article/another-article
I am developing the new site in asp.net mvc 5. I want to write an httpRedirect rule in web.config that can redirect any of the above url to home or any specific page.
so far this is how I am thinking of the solution
<location path="about-me">
<system.webServer>
<httpRedirect enabled="true" destination="/home"
httpResponseStatus="Permanent" />
</system.webServer>
</location>
But I have to write 100s of such entries in web.config. I am looking for a better and efficient alternative
In Global.asax, hook into the Application_BeginRequest method and redirect to the homepage from there
protected void Application_BeginRequest(object sender, EventArgs e)
{
try
{
if (HttpContext.Current.Request.Url.AbsolutePath.Contains("-")){
HttpContext.Current.Response.RedirectPermanent("/");
}
}
catch (ThreadAbortException){}
}
Optionally, you could hook into the Application_Error method inside Global.asax instead and detect 404's from there
protected void Application_Error(Object sender, EventArgs e)
{
var exception = Server.GetLastError();
if (exception != null && exception == 404)
{
try
{
Response.Clear();
Server.ClearError();
Response.Redirect("/");
}
}
catch (ThreadAbortException){}
}
}

Liferay : How can I land to custom url on signout

I'm not sure if I can achieve this by simply configuration or I need to override LogoutAction for it.
I've configured multiple organisation and each organisation has there own site which I want to navigate to my custom url for different site instead of default url on logout from liferay.
[EDITED]
I want to navigate on different url for each site, not a common url.
Thanks
For this you can use default.logout.page.path property (in portal-ext.properties file)
default.logout.page.path=
#default.logout.page.path=/web/guest/logout
I think you can achieve this by overriding LogoutPostAction through a hook.
Define your LogoutPostAction class in portal.properties of your hook:
logout.events.post=com.my.action.MyLogoutPostAction
Here is a sample code for the class to redirect to your desired page:
public class MyLogoutPostAction extends Action {
#Override
public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException {
try {
doRun(request, response);
}
catch (Exception e) {
throw new ActionException(e);
}
}
protected void doRun(HttpServletRequest request, HttpServletResponse response)
throws Exception {
long groupId = PortalUtil.getScopeGroupId(httpReq);
// code to fetch the Group
// ....
// ....
//
String postLogoutURL = "create your own URL";
// if required: add a parameter
postLogoutURL = HttpUtil.setParameter(postLogoutURL, "my_param", "my_param_value");
// redirect to that URL
response.sendRedirect(postLogoutURL);
}
}
The only thing that can be a road block with this approach would be if Liferay has lost context of the current group from which the user was logged-out. I have not tested the code.

ServiceStack Profile Steps not rendering

I have a ServiceStack Service with a service call like so:
public class MyService : Service
{
public object Get(MyServiceRequest request)
{
using (Profiler.Current.Step("Getting Data"))
{
// code that gets data
using (Profiler.Current.Step("Doing work with data"))
{
// code that does work
}
}
return response;
}
}
and a global.asax.cs like so:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.IsLocal)
Profiler.Start();
}
protected void Application_EndRequest(object sender, EventArgs e)
{
Profiler.Stop();
}
}
My problem is that when I test the service call through the browser I only see profile information for the overall request. "show time with children" and "show trivial" don't provider any more granular information. I've also placed breakpoints within each using statement to get a look at Profiler.Current and noticed in each case its Children property is null. Am I doing it wrong? Are they any other things I can do to troubleshoot this?
For some reason setting the level argument to Profile.Info in the Step method resolves my issue. That's weird because Profile.Info is the default if no argument is provided. Oh well. Moving on.

Resources