I have my site hosted on a subfolder (ApplicationPath). I have ISAPI Rewrite to translate from www.domain.com/Subfolder to www.domain.com. The problem is that asp.net MVC Html.ActionLink() (and similar functions) generates URLS with www.domain.com/Subfolder. I want to be able to remove the "/Subfolder" from the URL as the ISAPI Rewrite will take care of making the link work. How do i do this? Please, i've tried it like a million times with no success.
In your route class override the GetVirtualPath method having it add "../" to the beginning of the Url property of the VirtualPathData object returned by the base.GetVirtualPath call.
I don't believe there's a way to do this that is built in to the framework. You'll have to write your own Html helper extension method.
I think you should write your own Route class witch will rewrite URIs.
overriding the Route class didn't work, it doesn't handle the ApplicationPath. I think i have to override the GetVirtualPath of the RouteCollection class but i don't know how to do this.
Related
I would like to define a route as follows -
[Route("clients/{*code}/{id:guid}/update")]
public ActionResult Update(string code, Guid id)
{
}
Code will be something like "foo/bar/xyz".
Unfortunately, out-of-the-box MVC doesn't support greedy parameters in the middle of a Route definition.
This has previously been solved using the old MVC routing conventions, however I would like to have this as a RouteAtribute defintion.
As far as I know you cannot do it directly. However, you should be able to use IIS module UrlRewrite and rewrite the query with a greedy parameter in the middle to the one with a greedy parameter at the end.
So a client queries: clients/{*code}/{id:guid}/update
and your web api sees clients/{id:guid}/update/{*code}
From what I can tell there is no out-of-the-box way of doing this other than to use custom code like this example. Hope it helps.
I installed AttributeRouting via nuget, for an ASP.NET MVC 4 project, in which previously I mapped routes lowercase with a MapRouteLowercase extension, but I don't really think this could be causing the problem, because when I disabled my older route mappings in Global.asax, the attribute based routes were still not working.
[GET("Sample")]
public ActionResult Aszadba()
{
... do whatever
}
But when I check the routes.axd, the route is not present, nor it is working : (
Didn't touch any of the basic configurations that the package made at install. If I put a breakpoint into the AttributeRoutingConfig class it gets hit, so it seems the mapping function is called properly.
I also tried mapping with the [Route("Lofasz",HttpVerbs.GET)] format, without success.
Any help or hint would be appreciated!
I believe you need to use the latter [Route(Directory/Page)] attribute syntax, although I haven't seen the Get attribute before and it may be a valid alternative.
Crucially you need to add-
routes.MapMvcAttributeRoutes();
To your RouteConfig.cs file before your first mapped rotue.
I have found a dozens of threads about getting the name of the controller and method based on the url, I managed that just as well. Can I get the MethodInfo of the method based on their name automatically from the MVC engine, or do I have to do Type.GetType("Namespace.Controllers."+cname+"Controller").GetMethod(mname)? Which is not so nice, since how do I know the namespace in a framework class? How do I know if the default naming patterns are being observed, or is there a different config in use?
I want to get a "What Would MVC execute?" kind of result....
Is it possible?
EDIT: further info:
I have a framework which uses translatable, data-driven urls, and has a custom url rewriting in place. Now it works perfectly when I want to show the url of a news object, I just write #Url.Content("~/"+#Model.Link), and it displays "SomeNewsCategory/SomeNews" instead of "News/29" in the url, without the need to change the RouteTable.Routes dynamically. However in turn there is a problem when I try to write RedirectToAction("SomeStaticPage","Contact"); into a controller. For that, I need to register a static link in db, have it target "/SomeStaticPage/Contact", and then write
Redirect("~/"+DB.Load(linkid).Link); and that's just not nice, when I have 30 of these IDs. The web programmer guy in the team started registering "fake urls", which looked like this:
public class FakeURL
{
public string Controller;
public string Action;
public int LinkID;
}
and then he used it like Redirect(GetFakeUrl("controller","action")); which did the trick, but still was not nice. Now it got me thinking, if I apply a [Link(linkid)] attribute to each statically linked method, then override the RedirectToAction method in the base controller, and when he writes ReturnToAction("action","controller"), I'll actually look up the attribute, load the url, etc. But I'm yet to find a way to get the methodInfo based on the names of the controller and the action, or their url.
EDIT: I've written the reflection by myself, the only thing missing is getting my application's assembly from inside a razor helper, because the CallingAssembly is the dinamically compiled assembly of the .cshtml, not my WebApplication. Can I somehow get that?
To answer your edit, you can write typeof(SomeType).Assembly, where SomeType is any type defined in code in the project (eg, MvcApplication, or any model or controller)
Also, you can write ControllerContext.Controller.GetType() (or ViewContext) to get the controller type of the current request EDIT That's not what you're trying to do.
I found out that it was totally wrong approach. I tried to find the type of the controller based on the name, when instead I had the type all along.
So instead of #Url.Action("SomeAction","SomeController") I'll use #Url.MyAction((SomeController c)=>c.SomeAction()), so I won't even have to find the controller.
Is it possible to use [RequireHttps] filter with a different port other than 443? I can't find much documentation about it.
Steve Sanderson's approach finally worked fine for me. No need to use the MVC2 filters.
Subclass it and override HandleNonHttpsRequest(). From within that method, you can manipulate the URL as necessary. (For example, you can call base.HandleNonHttpsRequest(), then modify the filterContext.Result property to include a port number.) Then use [MyCustomRequireHttps] instead of [RequireHttps] on your actions.
Is it possible to define a route in the RouteCollection of Asp.net MVC, so that it just does the "URL rewriting" part and ignore the URL generation with Html.Actionlink(...)?
In fact I want to add a keyword between controller and action (controller/..keyword.../action) for certain very special requests. The generated URLs on the pages, however, should remain using the default routes. (controller/action)
Yes you can do what you are asking. You would just create your own route that inherited from the current one and override GetVirtualPath to always return null. This way no Action lookup would be done, but the URL would still function as a routing mapping to your action/controller.
Also by the way, what is happening isn't URL Rewriting, because you using the Routes to define endpoints in to your application. Or in other words an API. So no rewriting is taking place. Think of the relationship between routes and your action/controller as more of a publically defined namespace for the web. Just like a namespace you are defining a specific spot in your application where your action/controller can be found.