Url.Action is member of..? - asp.net-mvc

Url.Action found in MVC is member of what classes? VS2008 cannot find it and keeps offering me just System.Security.Policy to import. Alternatively, can I make ActionLink helper NOT to encode it's content, so this code would work:
sb.Append(helper.ActionLink(linkText, actionName, controllerName, new{onclick=""}));

System.Web.Mvc.UrlHelper

Please see System.Web.Mvc.UrlHelper (found in System.Web.Mvc.dll):
Contains methods for building URLs for
ASP.NET MVC within your application.

Related

Calling asp.net mvc controller's from normal aspx web form?

i want to make some question about asp.net mvc.
How i call asp.net mvc controller action from normal aspx web form?
Our project is used asp.net mvc framework with visual studio 2008 C#.net.
For eg,i want to use like this in normal aspx web form.
public ActionResult callMvc()
{
return RedirectToAction("Display","TempController");
}
I know it should not using like this way in MVC project,but,we need for some case.
Regards
Indi
You can just use a Response.Redirect with the right url which get routed to your controller in question, I guess in your case, that is:
Response.Redirect("/Temp/Display");
It would be a better idea to use the Url helper to resolve the url for you, that way if your routes change then you don't have to refactor your code. Use this method;
Response.Redirect(Url.Action("Display", "Temp"));
I have never used it in this context before but I believe it should work as expected.
Use following code:
Response.Redirect("~/Home/Index");
Controller/action
In WebForm cs file (class derived from System.Web.UI.Page) use:
this.Response.RedirectToRoute(new { controller="ControllerToRedirectTo", action="ActionToRedirectTo", id=5, SomeOtherActionParameter="Parameter value" });

Razor - how to cast from this.Page (dynamic WebPageBase.Page) to System.Web.UI.Page

I'm trying to use DotNetOpenAuth with Razor/MVC3. Most of DotNetOpenAuth HTML helpers are accepting System.Web.UI.Page as one of parameters, which works fine standard using WebForms engine but does not with Razor. Is it possible to cast dynamic WebPageBase.Page into System.Web.UI.Page somehow?
Thanks, Antonin
System.Web.UI.Page is part of the Web Forms page model and is totally unrelated to Razor. A Razor page inherits from System.Web.WebPages.WebPage which isn't part of the same hierarchy. As GvS mentioned, the "Page" property is a different object in Razor. In Razor, that property is just a C# dynamic object that provides a shortcut for accessing PageData values. For example: PageData["foo"] is the same as Page.foo.
They are totally different objects. So, no, you cannot cast or convert into a System.Web.UI.Page.
But this article might help you further.

Access code in a View via reflection

I have an HtmlHelper CmsEntry that is used like this
<%= Html.CmsEntry("stores.buyourstuff")%>
This helper is used lots of times and I want to generate a list of all views that contain this helper.
The list should contain the Viewname and the key ("stores.buyourstuff").
Is there a tool or some sample code that already does this?
Look at official site.
Creating Custom HTML Helpers : The Official Microsoft ASP.NET Site
HtmlHelper have ViewContext property. It can access ViewData and TempData.

How does ASP.Net MVC ActionLink Work?

I am trying to write my own LightWeight MVC for .Net 2.0 using NHaml as the view engine.
In ASP.Net 3.5 MVC the View file we used to specify the link by the code snippet.
Html.ActionLink("Add Product","Add");
In MVC binary there is no function to match this call.
I Only found:
(In class System.Web.Mvc.Html.LinkExtensions )
public static string ActionLink(this System.Web.Mvc.HtmlHelper htmlHelper,
string linkText, string actionName)
There are more similar static classes like FormExtensions, InputExtensions etc.
How does ASP.Net MVC handle it? Does it generates dynamic code for Html.ActionLink?
The ActionLink method is an extension method (hence the this before the type of the first parameter). This means you can use this method as an instance method on all HtmlHelper instances, even though it is not defined on HtmlHelper.
Html is a property on the View of type HtmlHelper. This means you can use the ActionLink extensionmethod on it.
The ActionLink method itself does nothing more than generate a link string (with regards to its arguments) and return that string.
Have you checked out the code on Codeplex? The MVC Framwork is open source, so you can dig around as much as you need to.

Prevent asp.net mvc from generating URLs with subfolders

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.

Resources