I made a folder called 'pages' in my asp web forms project. In this folder I have a lot of pages:
test.aspx
hello.aspx
When I open these pages in a browser I get:
www.domain.com/pages/test.aspx
www.domain.com/pages/hello.aspx
This is normal, I know. But what if I want to delete the /pages in the url and just show (without .aspx):
www.domain.com/test*.aspx*
www.domain.com/hello*.aspx*
I can do this by manually adding a new route (in RegisterRoutes() method) for each page but is there a way to do this dynamicly?
I found this question but I don't know if I can use it for this problem.
WebForms custom / dynamic routing
Try something like this, not sure if that will do it. But you could always just put your pages in the root directory. I think this would work.
Source Link: http://msdn.microsoft.com/en-us/library/vstudio/cc668177(v=vs.100).aspx
routes.MapPageRoute("PageRoute","{page}", "~/pages/{page}.aspx");
Not sure if that's what you were looking for, this way you can just do something like:
Response.RedirectToRoute("PageRoute", new { page = "test" });
Related
I have a project where all paths are routed through the same /index page, however this directs images/css/js files through the same page. In the example below, I am wondering how would I exclude those resources (i.e. /images/, /js/, /css/*, etc) from being routed to the /index page?
services.AddRazorPages().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/index", "{*url}");
});
Using this regex, I was able to route everything except if the path has "images/" and it seems to work:
options.Conventions.AddPageRoute("/Index", "{*url:regex(^(?!images/).*$)}");
routes.MapPageRoute("Main", "", "~/index.aspx");
thats the route I mapped on index page..
when I call the url with index.aspx it displays like
www.abc.com/index.aspx
but I want it to show
www.abc.com
even when index.aspx is called
Regarding my comment, URL Rewrite is also available in IIS and Asp.net. So you can potentially use it.
Another solution would be to redirect to your route. The route alone doesn't change the URL, it just allows you to access the resource via the defined route.
You can redirect to the route though, which will rewrite the URL on the client
For example like this:
if (Request.Path != "/")
{
Context.Response.RedirectToRoute("Main");
}
This is very simplified and might not work in all scenarios, so please be very careful.
I have a mvc3 web application which will be delopyed to an directory of a website. the address of the application seems like http://wwww.xx.com/aopo/. aopo is the name of the directory. After the deployement I find that the url in js file does not work any more. because I had write the js code with hard code url, just like
$.ajax({url:"/employee/getemployee"})
In that case the request will be sent to http://wwww.xx.com/employee/getemployee instead of http://wwww.xx.com/aopo/employee/getemployee, and 404 is returned.
There is lots of code in js like that. I don't want to modify the js. Is there any simple approach to make it work? Can I rewrite the request in global.asax.cs?
I believe you'll need to change your url references to this:
$.ajax({url:"#Url.Action("getemplotee","employee")"})
If this is a script file you can write the correct URL into an element on the page and read that:
<div id='Main' data-url='#Url.Action("getemplotee","employee")' />
Then in your script:
$.ajax({url:$('#Main').data('url')})
Rgarding:
There is lots of code in js like that. I don't want to modify the js
I understand, but you need to use the proper helper methods to account for running under virtual directories, so I would recommending biting the bullet and just fixing it the right way.
One not so clean solution might be,
Setting a url prefix in the layout(in a common place), like
<script type="text/javascript">
#{
string root = Url.Content("~/");
if (root.EndsWith("/", StringComparison.Ordinal))
{
root = root.Substring(0, root.Length - 1);
}
}
window.URL_PREFIX = '#root';
</script>
then use the prefix in the ajaxSend hook and change the url.
$(document).ajaxSend(function (event, jqxhr, settings) {
if (window.URL_PREFIX && settings.url.indexOf(window.URL_PREFIX)) {
settings.url = window.URL_PREFIX + settings.url;
}
});
note: If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxSend() method will not fire.
its not an ideal solution, but might be helpful in a tight situation.
hope this helps.
No, that's not possible. Since the request doesn't arrive in your application you can't access it and thus you can't rewrite it.
If you have control over the root site, you could use the IIS rewrite module to rewrite the URL to your website.
I've a domain name called mywebsite.com but I prefer users to access to my website through the www subdomain.
How can I achieve a verification and redirection in asp.net mvc3 easily?
When I was using php I did something like that :
if($_SERVER['SERVER_NAME'] != "www.mywebsite.com")
header('Location: www.mywebsite.com');
I'd like to find a similar way to achieve this kind of redirection in asp.net (C#) (I'd prefer not to set a 301 redirection but just a soft redirection like the one I was using in PHP).
I know I could do a verification in each of my controllers action methods and the redirect the user if he is not on the subdomain www.mywebsite.com but I'd prefer to write once the verification and the redirection and I can't find where :/
Thanks a lot !
You could use:
Request.Url.ToString()
This will return the URL , then you can quickly check if it contains 'www.' and redirect if true. However I would suggest that your handle this in the URL rewrite in IIS.
Follow the guide here: http://www.dotnetexpertguide.com/2011/08/iis-7-redirect-domaincom-to.html this is for making domain.com go to www.domain.com, but it works the same way for the opposite.
you could try something like this
if(!Request.Url.Host.Equals("www.mywebsite.com"))
{
Response.Redirect("website");
}
If you are looking to just add/append header information, then use Response.AppendHeader().
The Request.Url.ToString() property can be checked for the url you are looking for as per Ryan's answer.
Actually, the original question asks specifically for a 'soft' redirection (201 or 202) instead of a Moved Permanently/Found (301/302), so i think that instead of Response.Redirect the conditional line should be:
if(!Request.Url.Host.Equals("www.mywebsite.com"))
{
HttpContext.Current.Response.Headers.Add("Location", "www.mywebsite.com");
}
EDIT: I believe the status may also be set directly, used in conjunction with Response.Redirect:
if(!Request.Url.Host.Equals("www.mywebsite.com"))
{
HttpContext.Current.Response.Redirect("www.mywebsite.com");
HttpContext.Current.Response.Status = "201 Created";
}
If I am not totally missing something here, can't you just setup the host-header in the IIS settings for the site to include both www.domain.com as well as domain.com?
In the global.asax of my website project (not MVC, not Web Application) MapPageRoute won't let me map to a page with an extension.
For example:
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}", "~/GetMobilePass.aspx");
works as expected, but
routes.MapPageRoute("GetMobilePassForAttendee", "Pass/Attendee/{attendeeId}/pass.pkpass", "~/GetMobilePass.aspx");
returns a 404.
Anyone know why?
Perhaps I should be using URL rewriting, but all the material I've read has suggested I use routing instead.
Have you tested the "pass.pkpass" page separately, let say attendeeid=1 to see if Pass/Attendee/1/pass.pkpass is a working page without the routing?
Because .NET routing would not work in this case if this is a working page. It will skip the routing and load the actual "Pass/Attendee/1/pass.pkpass" page.
This worked for me:
routes.MapPageRoute("FederationMetadataRoute", "FederationMetadata/2007-06/{file}", "~/FederationMetadata/2007-06/FederationMetadata.aspx")
And then I just check the {file} parameter to make sure it equaled FederationMetadata.xml
So in your case, just set Pass/Attendee/{attendeeId}/pass.pkpass to Pass/Attendee/{attendeeId}/{pkpass}
and check {pkpass} to make sure it equals pass.pkpass on your page.