asp.net - How to achieve URL rewrite in mvc3? -
my directory structure in mvc3 below.
i installed url-rewriting 2.0. have added url rewriting.
web.config
<?xml version="1.0" encoding="utf-8"?> <!-- more information on how configure asp.net application, please visit http://go.microsoft.com/fwlink/?linkid=152368 --> <configuration> <configsections> <sectiongroup name="elmah"> <section name="security" requirepermission="false" type="elmah.securitysectionhandler, elmah" /> <section name="errorlog" requirepermission="false" type="elmah.errorlogsectionhandler, elmah" /> <section name="errormail" requirepermission="false" type="elmah.errormailsectionhandler, elmah" /> <section name="errorfilter" requirepermission="false" type="elmah.errorfiltersectionhandler, elmah" /> </sectiongroup> <section name="dotless" type="dotless.core.configuration.dotlessconfigurationsectionhandler, dotless.core" /> <section name="combres" type="combres.configsectionsetting, combres, version=2.2, culture=neutral, publickeytoken=1ca6b37997dd7536" /> <sectiongroup name="dotnetopenauth" type="dotnetopenauth.configuration.dotnetopenauthsection, dotnetopenauth.core"> <section name="messaging" type="dotnetopenauth.configuration.messagingelement, dotnetopenauth.core" requirepermission="false" allowlocation="true" /> <section name="reporting" type="dotnetopenauth.configuration.reportingelement, dotnetopenauth.core" requirepermission="false" allowlocation="true" /> <section name="openid" type="dotnetopenauth.configuration.openidelement, dotnetopenauth.openid" requirepermission="false" allowlocation="true" /> <section name="oauth" type="dotnetopenauth.configuration.oauthelement, dotnetopenauth.oauth" requirepermission="false" allowlocation="true" /> </sectiongroup> <section name="glimpse" type="glimpse.core.configuration.section, glimpse.core" /> </configsections> <connectionstrings> <add name="applicationservices" connectionstring="data source=.\sqlexpress;integrated security=sspi;attachdbfilename=|datadirectory|aspnetdb.mdf;user instance=true" providername="system.data.sqlclient" /> </connectionstrings> <appsettings> <add key="webpages:version" value="1.0.0.0" /> <add key="clientvalidationenabled" value="true" /> <add key="unobtrusivejavascriptenabled" value="true" /> <add key="elmah.mvc.disablehandler" value="false" /> <add key="elmah.mvc.disablehandleerrorfilter" value="false" /> <add key="elmah.mvc.requiresauthentication" value="false" /> <add key="elmah.mvc.ignoredefaultroute" value="false" /> <add key="elmah.mvc.allowedroles" value="*" /> <add key="elmah.mvc.allowedusers" value="*" /> <add key="elmah.mvc.route" value="elmah" /> <add key="combressectionname" value="combres" /> </appsettings> <system.web> <customerrors mode="remoteonly"></customerrors> <compilation debug="true" targetframework="4.0"> <assemblies> <add assembly="system.web.abstractions, version=4.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" /> <add assembly="system.web.helpers, version=1.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" /> <add assembly="system.web.routing, version=4.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" /> <add assembly="system.web.mvc, version=3.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" /> <add assembly="system.web.webpages, version=1.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35" /> </assemblies> </compilation> <authentication mode="forms"> <forms loginurl="~/account/logon" timeout="2880" /> </authentication> <membership> <providers> <clear /> <add name="aspnetsqlmembershipprovider" type="system.web.security.sqlmembershipprovider" connectionstringname="applicationservices" enablepasswordretrieval="false" enablepasswordreset="true" requiresquestionandanswer="false" requiresuniqueemail="false" maxinvalidpasswordattempts="5" minrequiredpasswordlength="6" minrequirednonalphanumericcharacters="0" passwordattemptwindow="10" applicationname="/" /> </providers> </membership> <profile> <providers> <clear /> <add name="aspnetsqlprofileprovider" type="system.web.profile.sqlprofileprovider" connectionstringname="applicationservices" applicationname="/" /> </providers> </profile> <rolemanager enabled="false"> <providers> <clear /> <add name="aspnetsqlroleprovider" type="system.web.security.sqlroleprovider" connectionstringname="applicationservices" applicationname="/" /> <add name="aspnetwindowstokenroleprovider" type="system.web.security.windowstokenroleprovider" applicationname="/" /> </providers> </rolemanager> <pages> <namespaces> <add namespace="system.web.helpers" /> <add namespace="system.web.mvc" /> <add namespace="system.web.mvc.ajax" /> <add namespace="system.web.mvc.html" /> <add namespace="system.web.routing" /> <add namespace="system.web.webpages" /> <add namespace="combres" /> <add namespace="combres.mvc" /> </namespaces> </pages> <httphandlers> <add verb="post,get,head" path="elmah.axd" type="elmah.errorlogpagefactory, elmah" /> <add path="*.less" verb="get" type="dotless.core.lesscsshttphandler, dotless.core" /> <add path="glimpse.axd" verb="get" type="glimpse.aspnet.httphandler, glimpse.aspnet" /> </httphandlers> <!-- glimpse: can commented in add additional data trace tab when using webforms <trace writetodiagnosticstrace="true" enabled="true" pageoutput="false"/> --> <httpmodules> <add name="errorlog" type="elmah.errorlogmodule, elmah" /> <add name="errormail" type="elmah.errormailmodule, elmah" /> <add name="errorfilter" type="elmah.errorfiltermodule, elmah" /> <add name="glimpse" type="glimpse.aspnet.httpmodule, glimpse.aspnet" /> </httpmodules> </system.web> <system.webserver> <rewrite> <rules> <rule name="webformtomypage" stopprocessing="true"> <match url="^webform1.aspx" /> <action type="redirect" url="mydirectory/mypage" appendquerystring="false" /> </rule> </rules> </rewrite> <validation validateintegratedmodeconfiguration="false" /> <modules runallmanagedmodulesforallrequests="true"> <add name="errorlog" type="elmah.errorlogmodule, elmah" precondition="managedhandler" /> <add name="errormail" type="elmah.errormailmodule, elmah" precondition="managedhandler" /> <add name="errorfilter" type="elmah.errorfiltermodule, elmah" precondition="managedhandler" /> <add name="glimpse" type="glimpse.aspnet.httpmodule, glimpse.aspnet" precondition="integratedmode" /> </modules> <handlers> <add name="dotless" path="*.less" verb="get" type="dotless.core.lesscsshttphandler,dotless.core" resourcetype="file" precondition="" /> <add name="glimpse" path="glimpse.axd" verb="get" type="glimpse.aspnet.httphandler, glimpse.aspnet" precondition="integratedmode" /> </handlers> </system.webserver> <runtime> <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentassembly> <assemblyidentity name="system.web.mvc" publickeytoken="31bf3856ad364e35" /> <bindingredirect oldversion="1.0.0.0-2.0.0.0" newversion="3.0.0.0" /> </dependentassembly> <dependentassembly> <assemblyidentity name="ajaxmin" publickeytoken="21ef50ce11b5d80f" culture="neutral" /> <bindingredirect oldversion="0.0.0.0-4.84.4790.14405" newversion="4.84.4790.14405" /> </dependentassembly> <dependentassembly> <assemblyidentity name="system.net.http" publickeytoken="b03f5f7f11d50a3a" culture="neutral" /> <bindingredirect oldversion="0.0.0.0-2.2.13.0" newversion="2.2.13.0" /> </dependentassembly> </assemblybinding> <!-- prevents windows event log logging hmac1 being used (when other party needs it). --> <legacyhmacwarning enabled="0" /> <!-- when targeting asp.net mvc 3, assemblybinding makes mvc 1 , 2 references relink mvc 3 libraries such dotnetopenauth compile against mvc 1 work it. <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentassembly> <assemblyidentity name="system.web.mvc" publickeytoken="31bf3856ad364e35" /> <bindingredirect oldversion="1.0.0.0-3.0.0.0" newversion="3.0.0.0" /> </dependentassembly> </assemblybinding> --> </runtime> <elmah></elmah> <dotless minifycss="false" cache="true" web="false" /> <combres definitionurl="~/app_data/combres.xml" /> <system.net> <defaultproxy enabled="true" /> <settings> <!-- setting causes .net check certificate revocation lists (crl) before trusting https certificates. setting tends not allowed in shared hosting environments. --> <!--<servicepointmanager checkcertificaterevocationlist="true"/>--> </settings> </system.net> <dotnetopenauth> <messaging> <untrustedwebrequest> <whitelisthosts> <!-- uncomment enable communication localhost (should not activate in production!) --> <!--<add name="localhost" />--> </whitelisthosts> </untrustedwebrequest> </messaging> <!-- allow dotnetopenauth publish usage statistics library authors improve library. --> <reporting enabled="true" /> <!-- optional configuration section aspects of dotnetopenauth can customized. --> <!-- complete set of configuration options see http://www.dotnetopenauth.net/developers/code-snippets/configuration-options/ --> <openid> <relyingparty> <security requiressl="false"> <!-- uncomment trustedproviders tag if relying party should accept positive assertions closed set of openid providers. --> <!--<trustedproviders rejectassertionsfromuntrustedproviders="true"> <add endpoint="https://www.google.com/accounts/o8/ud" /> </trustedproviders>--> </security> <behaviors> <!-- following optional behavior allows rps use sreg only, compatible ops use attribute exchange (in various formats). --> <add type="dotnetopenauth.openid.relyingparty.behaviors.axfetchassregtransform, dotnetopenauth.openid.relyingparty" /> </behaviors> </relyingparty> <provider></provider> </openid> </dotnetopenauth> <uri> <!-- uri section necessary turn on .net 3.5 support idn (international domain names), necessary openid urls unicode characters in domain/host name. required put uri class rfc 3986 escaping mode, openid , oauth require. --> <idn enabled="all" /> <iriparsing enabled="true" /> </uri> <glimpse defaultruntimepolicy="on" endpointbaseuri="~/glimpse.axd"> <!-- if having issues glimpse, please include this. figure out whats going on. <logging level="trace" />--> <!-- want use glimpse on remote server? ignore localpolicy removing comment.--> <runtimepolicies> <ignoredtypes> <add type="glimpse.aspnet.policy.localpolicy, glimpse.aspnet"/> </ignoredtypes> </runtimepolicies> </glimpse> </configuration>
when trying type below url
http://localhost/webform1.aspx
it should turned below...
http://localhost/mydirectory/mypage
how can achieve this?
as far can tell, want 2 things:
- when user navigates
/mydirectory/mypage
want return whateverwebform1.aspx
renders. - when user navigates
/webform1.aspx
want redirect him/mydirectory/mypage
url in address bar changes.
for first one, add route before default route:
routes.mappageroute("mypage", "mydirectory/mypage", "~/webform1.aspx");
this make /mydirectory/mypage
alias /webform1.aspx
.
for second one, add following rewrite rule system.webserver
section of web.config
file:
<rewrite> <rules> <rule name="webformtomypage" stopprocessing="true"> <match url="^webform1.aspx" /> <action type="redirect" url="mydirectory/mypage" /> </rule> </rules> </rewrite>
Comments
Post a Comment