简体   繁体   中英

Removing .aspx extension from sitecore in ExecuteRequest pipeline

Currently I am trying to remove .aspx extensions from sitecore URL's. A blog suggests that changing the following would fix this issue:

<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="true"

The problem is that this does not work for all code in our websites as some custom controls force the usage of .aspx.

What I was thinking is that if I can get into the pipeline before a response is sent back to the user I could change the URL to one that is more SEO friendly (no .aspx), I am trying to do this using the following code

public class CustomExecuteRequest : Sitecore.Pipelines.HttpRequest.ExecuteRequest
{
    protected override void PerformRedirect(string url)
    {
        base.PerformRedirect(url.Replace(".aspx" , ""));
    }

    public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
    {
        PerformRedirect(args.Url.FilePathWithQueryString.Replace(".aspx", ""));

        //args.Url.FilePathWithQueryString = args.Url.FilePathWithQueryString.Replace(".aspx", "");

        //base.Process(args);
    }
}

The code is being hit and this solution works, however it is performing a redirect which is really not what I want to do, I would rather just have a regular request with the URL cleaned up. The problem is that the args variable has a read-only field (commented out below) that is used to actually create the URL.

Does anyone have any ideas as to how I can change the URL here, or perhaps somewhere else in the pipeline?

What you are doing inside the HttpRequestPipeline is certainly NOT the correct way to change links. As with any web application, any links on the page are returned to the client as content of the HTTP request. Redirecting during the request will not change the rendered content.

The only way to do this is the hard way:

  • Find all the places in your source generating the links containing .aspx. A search for "aspx" inside the solution might help
  • Find all references to UrlOptions and to LinkManager and check if AddAspxExtension=true is set manually as urlOptions.

Something like this would always produce a .aspx ending for example:

var urlOptions = UrlOptions.DefaultOptions;
urlOptions.AddAspxExtension = true;

LinkManager.GetItemUrl(item, urlOptions);

更改所有链接以使用链接管理器的困难方法,或者如果您时间有限,则建议使用Sitecore之外的方法并使用IIS urlrewrite模块。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM