Royi Namir
Royi Namir

Reputation: 148634

Prevent access to aspx files and allow access only via routing?

I have a url route which I declare at global.asax file :

  routes.MapPageRoute("RouteAdmin", "Admin/{Url}", "~/pages/MyPage.aspx", false);

But if the user tries to access mysite.com/pages/MyPage.aspx he can still see the page

Question :

Upvotes: 3

Views: 2117

Answers (2)

VahidN
VahidN

Reputation: 19166

In ASP.NET MVC views are not accessible directly by defining them using a not found handler:

<system.web>
  <httpHandlers>
    <remove verb="*" path="*.aspx" />
    <add path="*.aspx" verb="*" type="System.Web.HttpNotFoundHandler" />
  </httpHandlers>
</system.web>

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <handlers>
    <add name="BlockViewHandler" path="*.aspx" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  </handlers>
</system.webServer>

Upvotes: 1

Royi Namir
Royi Namir

Reputation: 148634

Found it.

If I access directly to the aspx file so I can check this prop:

Page.RouteData.RouteHandler is null

where

if I use route url it is not null :

{System.Web.Routing.PageRouteHandler}

Edit

(better solution)

Add those 2 lines to global asax

 routes.MapPageRoute("Route", "{*.}", "~/pages/default.aspx", false );
 routes.RouteExistingFiles = true; 

Upvotes: 0

Related Questions