0

I have pages set up with razor code to use a consistent layout on every page. I don't want the pages url to display with the .cshtml tag How can I do this. I use JQuery, JavaScript, HTML, CSS, ASP.net (web Pages Logic)

EX URL; http:// www.site.com/page.htm

I am new to ASP and server side stuff.

@{
    Layout = "~/Shared/_Layout1.cshtml"; <!--i use a _PageStart.cshtml that contains this line for dry purpose-->

    Page.title = ""; @*add pages title here*@
}

<!--If you want to add Page specific stylesheets Link them here with HTML tags-->
@section ExtraHeadContent {

}


@section NavBarLinks {
    <!--Enter links here for the top navigation bar <a class="topnav-link" href="~/index.html">| Home</a> -->
}

<!--Enter the Pages HTML content here. This will be displayed in the white content div-->

If there are any relevant pages or discussions you've found in the past please share.

Noah
  • 9
  • 8
  • 1
    Use extensionless routes? – asawyer Jun 30 '14 at 15:45
  • 4
    In IIS change your handler mappings – Ashley Medway Jun 30 '14 at 15:46
  • I don't know what any of that is Ashley. Sorry I'm new to server side – Noah Jun 30 '14 at 15:55
  • 1
    Have you actually run any code to see the URL? By default at least for any of the more recent versions of MVC and/or IIS URLs don't have extensions period. (Hence asawyer's extensionless routes comment) .cshtml is the extension of the file for views, but that shouldn't be what's showing up when actually browsing the site – Kritner Jun 30 '14 at 16:15
  • I'm using web pages logic not mvc, and yes I have run it live. the only page that will display without the .chshtml extension currently is the defaulted homepage which displays "www.example.com/" every other page I wrote with the razor code "which is all of them" displays "www.example.com/PageName.cshtml – Noah Jun 30 '14 at 16:43
  • If you can, just change the actual file extensions. mv myfile.cshtml myfile.htm – Qaz Jun 30 '14 at 16:50
  • You could use the ASP.NET [Friendly URL's package](http://www.nuget.org/packages/Microsoft.Aspnet.FriendlyUrls/). It contains automatic configuration to hide the extensions, as well as some handy mobile features (you don't have to use those). And it works with Web Forms. – mason Jul 08 '14 at 19:11

1 Answers1

1

The built in routing system within the ASP.NET Web Pages framework allows you to omit the file extension altogether. If you request www.yourdomain.com/YourPage (where YourPage represents a file called YourPage.cshtml), it should work by default. You can read more about it here: http://www.mikesdotnetting.com/Article/165/WebMatrix-URLs-UrlData-and-Routing-for-SEO.

As for replacing the file extension with .htm, I'm not sure that you hope to achieve by doing that. It won't improve SEO. However, one option would be to use a Nuget package called WebPageRouteHandler. I've written about that too: http://www.mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages. Mapping each file individually could be a pain, but you could generate the routes dynamically from a database or from looping through the physical files to reduce the code required.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • Your solution works For Web Matrix, but when I tested on my server it gave me a 404 error. I am using an IIS6 server. Do you know what would cause this? No need for htm i just need to remove the extension for a friendly url – Noah Jul 08 '14 at 18:57
  • With IIS 6, you need to add wildcard mapping: https://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/5c5ae5e0-f4f9-44b0-a743-f4c3a5ff68ec.mspx?mfr=true – Mike Brind Jul 09 '14 at 08:50