TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Kalyani Shevale
NA
3.2k
703.9k
how to add sitemap.xml file in mvc
Mar 12 2020 5:45 AM
I have added sitemap file in my projects.
below is sitemap controller.
public
class
SitemapController : Controller
{
// GET: Sitemap
public
ActionResult Index()
{
StringBuilder sitemap =
new
StringBuilder();
sitemap.AppendLine(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
);
sitemap.AppendLine(
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">"
);
BuildSitemap(
new
CmsPages().GetAllPages(),
new
Languages().GetAllLanguages(isActive:
true
),
new
GlobalConfigurations().GetGlobalConfiguration(),
null
,
ref
sitemap);
sitemap.AppendLine(
"</urlset>"
);
return
Content(sitemap.ToString(),
"text/xml"
);
}
private
static
void
BuildSitemap(List<CmsPage> cmsPageList, List<Language> sitemapLanguages, GlobalConfiguration globalConfiguration,
int
? pageParentId,
ref
StringBuilder sitemap)
{
if
(cmsPageList.IsNotNull())
{
List<CmsPage> sitemapPages = (from page
in
cmsPageList
where page.PageParentId == pageParentId
&& page.ShowInSitemap
&& page.IsActive
select page).ToList();
if
(sitemapPages.IsNotNull() && sitemapPages.Count() > 0)
{
foreach
(CmsPage page
in
sitemapPages)
{
foreach
(Language language
in
sitemapLanguages)
{
sitemap.AppendLine(
" <url>"
);
sitemap.AppendLine(
" <loc>"
+ globalConfiguration.DomainName.ToUrl() + (page.IsHomePage ? (language.LanguageCode != globalConfiguration.DefaultLanguageCode ? language.LanguageCode +
"/"
:
""
) : language.LanguageCode +
"/"
+ page.FullSegment +
"/"
) +
"</loc>"
);
foreach
(Language languageSubset
in
sitemapLanguages)
{
sitemap.AppendLine(
" <xhtml:link rel=\"alternate\" hreflang=\""
+ languageSubset.LanguageCode +
"\" href=\""
+ globalConfiguration.DomainName.ToUrl() + (page.IsHomePage ? (languageSubset.LanguageCode != globalConfiguration.DefaultLanguageCode ? languageSubset.LanguageCode +
"/"
:
""
) : languageSubset.LanguageCode +
"/"
+ page.FullSegment +
"/"
) +
"\" />"
);
}
sitemap.AppendLine(
" </url>"
);
}
BuildSitemap(cmsPageList, sitemapLanguages, globalConfiguration, page.PageId,
ref
sitemap);
}
}
}
}
}
route.config code for sitemap
routes.MapRoute(
name:
"Sitemap"
,
url:
"sitemap.xml"
,
defaults:
new
{ controller =
"Sitemap"
, action =
"Index"
}
);
web.config site map url
<
handlers
>
<
add
name
=
"SitemapXmlHandler"
path
=
"sitemap.xml"
verb
=
"GET"
type
=
"System.Web.Handlers.TransferRequestHandler"
/>
</
handlers
>
please where is issue for sitemap.
Reply
Answers (
1
)
create an application to calculate bowling score
Joining table does not accept null values. mvc5 c#