开发者

Ajax.ActionLink without href attribute for better SEO

开发者 https://www.devze.com 2023-03-24 13:56 出处:网络
I am using Ajax.ActionLink to display an hyperlink and when clicked make an ajax request (for example the flag hyperlink in this site, report, etc) and put the result in some div, this all can be easi

I am using Ajax.ActionLink to display an hyperlink and when clicked make an ajax request (for example the flag hyperlink in this site, report, etc) and put the result in some div, this all can be easily done with this method, but the problems comes with SEO, because this hyperlink actually have and href attribute and the spider follow the url.

I wa开发者_StackOverflow中文版nt an anchor without href to make it SEO friendly and extension method to do all that, but with all the overloads of Ajax.ActionLink example:

Ajax.SEOFriendlyActionLink("my hyperlink", "action", "controller" ... more options)

this can generate something like this

<a urlForAjax="url here">my hyperlink</a>

Of course making the ajax callbacks.

Is there any thing like this out there?


The solution is as follows:

Ajax.ActionLink("my hyperlink", "", "" ... new AjaxOptions() { Url = Url.Action("action", "controller") ... })

It will generate something like this: < a href="/" data-ajax-url="url here" ...> </a>


The solution given by @kyw is good but have one drawback. The link can be opened in newtab which is propably not the behaviour we are looking for.

We will change code a little :

Ajax.ActionLink("my hyperlink", "", "" ... new AjaxOptions() { Url = Url.Action("action", "controller") ... }, new { href = "!!"})

Now we need some jQuery :

$('a[href$="!!"]').each(function (index, element) {
element.removeAttribute("href");
});

From now on every link which href is ending on !! will have this attribute removed, thus it will disable option to open link in new tab.

0

精彩评论

暂无评论...
验证码 换一张
取 消