Having an #WebServlet(urlPatterns = "/myServlet/"). If the user goes to myapp/myServlet/other, I still want my servlet to catch. So to say, wildcard anything on after the servlet path. How could I do this?
You can use * as prefix or suffix wildcard. In your case, you can use /myServlet/* for a folder mapping.
#WebServlet("/myServlet/*")
The path info (the part after the mapping in the URL) is in the servlet by the way available as:
String pathInfo = request.getPathInfo();
This would in case of myapp/myServlet/other return /other.
See also:
Servlet and path parameters like /xyz/{value}/test, how to map in web.xml?
use "/myServlet/*" as your servlet mapping.
Related
The request mapping for my controller is something like this:
/hospital/{hospitalId}/department/{departmentId}/doctors
And i tried to add the pattern for authentication required filter:
/hospital/*/department/*/doctors
But it's not working. It's there a chance to make this work?
The mapping for a filter isn't a Ant-style mapping as you're used to in Spring, but a mapping as defined in the Servlet specification. In section 12.2 it says:
the following syntax is used to define mappings:
A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
A string beginning with a ‘*.’ prefix is used as an extension mapping.
The empty string ("") is a special URL pattern that exactly maps to the
application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’/’ and the servlet path and context path is empty string (““).
A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.
/hospital/*/department/*/doctors only meets the criteria of the final bullet so it's treated as an exact match.
The best that you can do within confines of the servlet specification is to use /hospital/* and then do some secondary matching in your filter's code. You could use Spring Framework's org.springframework.util.AntPathMatcher to do so.
I'm using Filter and saving a mapping with all urls (and number of times each url was called).
#WebFilter(filterName = "SessionFilter", urlPatterns = {"/*"})
public class SessionFilter implements Filter {...}
I have this : #WebServlet(/test/aaa) so I expect the filter to get the request and forward it to my service. The issue is that if I send some post fake request like : "testing/lalalal" - it passes the filter and then I insert it to my map (which should not happen if I don't have valid mapping for it)
I tried using urlValidator but it didn't seem to help. I also tried to find if I get some error (404) but don't know where to look.
can anyone advise?
Thanks!
Instead of mapping the filter to an overly generic URL pattern, map it to URLs and/or servlets of actual interest. Below example maps it to a specific servlet.
#WebFilter(filterName = "sessionFilter", servletNames = {"yourServlet"})
Don't forget to give your servlet a name.
#WebServlet(servletName = "yourServlet", urlPatterns = {"/test/aaa"})
Im adding the filevistacontrol to my asp.net MVC web application.
I have a media.aspx page that is ignored in the routing with
routes.IgnoreRoute("media.aspx");
This works successfully and serves a standard webforms page.
Upon adding the filevistacontrol, I can't seem to ignore any calls the control makes to it's webservice.
Eg the following ignoreRoute still seems to get picked up by the MvcHandler.
routes.IgnoreRoute("FileVistaControl/filevista.asmx/GetLanguageFile/");
The exception thrown is:
'The RouteData must contain an item named 'controller' with a non-empty string value'
Thanks in advance.
Short answer:
routes.IgnoreRoute( "{*url}", new { url = #".*\.asmx(/.*)?" } );
Long answer:
If your service can be in any level of a path, none of these options will work for all possible .asmx services:
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
By default, the parameters in a route pattern will match until they find a slash.
If the parameter starts with a star *, like pathInfo in those answers, it will match everything, including slashes.
So:
the first answer will only work for .asmx services in the root path, becasuse {resource} will not match slashes. (Would work for something like http://example.com/weather.asmx/forecast)
the second one will only work for .asmx services which are one level away from the root.{directory} will match the first segment of the path, and {resource} the name of the service. (Would work for something like http://example.com/services/weather.asmx/forecast)
None would work for http://example.com/services/weather/weather.asmx/forecast)
The solution is using another overload of the IgnoreRoute method which allows to specify constraints. Using this solution you can use a simple pattern which matches all the url, like this: {*url}. Then you only have to set a constraint which checks that this url refers to a .asmx service. This constraint can be expressed with a regex like this: .*\.asmx(/.*)?. This regex matches any string which ends with .asmx optionally followed by an slash and any number of characters after it.
So, the final answer is this:
routes.IgnoreRoute( "{*url}", new { url = #".*\.asmx(/.*)?" } );
I got it to work using this (a combo of other answers):
routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
What happens when you use:
routes.IgnoreRoute("FileVistaControl/filevista.asmx");
If that doesn't work, try using the ASP.NET Routing Debugger to help you:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Try this:
routes.IgnoreRoute("{*filevista}", new { filevista = #"(.*/)?filevista.asmx(/.*)?" });
This is based on a Phil Haack recommendation stated here.
Have you tried:
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
It would help if you posted the source for your route configuration. I'm going to take a shot in the dark and say to make sure that your IgnoreRoute() calls are all at the top of your routing definition.
The way IgnoreRoute works is to create a route that matches the ignored route URL and constraints, and attaches a StopRoutingHandler as the RouteHandler. The UrlRoutingModule knows that a StopRoutingHandler means it shouldn't route the request.
As we know, the routes are matched in the order of which they are defined. So, if your {controller}/{action}/{id} route appears before your "FileVistaControl/filevista.asmx/GetLanguageFile/" route, then it will match the "{controller}/{action}/{id}" route.
I may be totally off base here, but it's hard to know without seeing your source. Hope this helps. And post source code! You'll get better answers.
In ASP.NET MVC how do I get the fully qualified path to my css file
by specifying the relative path.
Eg
Url.Content("~/Content/Print.css")
This returns eg "/Content/Print.css"
Where as I want
http://www.mysite.com/Content/Printcss
Understand the issue?
Malcolm
Similar to Phil, I would use the Request object. However, I would look at the Url property.
With the Url, you can call GetLeftPart(UriPartial.Authority) to get the missing part of your address:
string address =
System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) +
Url.Content("~/Content/Print.css");
The GetLeftPart should return "http://www.mysite.com" as shown in the doc:
http://msdn.microsoft.com/en-us/library/system.uri.getleftpart(v=VS.100).aspx
I'd probably concatenate Request.UserHostName and your CSS location:
String.Format("{0}/Content/Print.css", Request.UserHostName);
Part of my application maps resources stored in a number of locations onto web URLs like this:
http://servername/files/path/to/my/resource/
The resources location is modelled after file paths and as a result there can be an unlimited level of nesting. Is it possible to construct an MVC route that matches this so that I get the path in its entirety passed into my controller? Either as a single string or possibly as an params style array of strings.
I guess this requires a match on the files keyword, followed by some sort of wildcard. Though I have no idea if MVC supports this.
A route like
"Files/{*path}"
will get the path as a single string. The * designates it as a wildcard mapping and it will consume the whole URL after "Files/".
For more information on ASP.NET's Routing feature, please see MSDN:
http://msdn.microsoft.com/en-us/library/cc668201.aspx
And for the "catch-all" parameters you want to use, see the section under "Handling a Variable Number of Segments".