I am developing a MVC 3 application for a corporate intranet site and I am having some issues with the URL helper sometimes not producing correct URLs. The application is being accessed through an access manager application that is controlled by our IT department which basically provides a standardized URL so that the user does not need to know any information about the server. For example, to access the application directly on the server, I would visit:
http://philsserver/App
Through the access manager, I would use the URL provided by the IT department:
http://secureintranet/PHILSAPP/App/
I am using the MVC URL helper in several places in my application - the issue is that sometimes the "PHILSAPP" part is left out - when I use it within an "<a>" link, it works, but when I use it in elsewhere, it does not.
For example, the code:
<a href="#Url.Action("Index", "FormsAndTemplates")">
correctly creates the link as:
<a href="/PHILSAPP/App/FormsAndTemplates">.
The following code:
#Html.TextBox("lastName", ViewBag.LastName as string, new { #class = "input-mini", #autocomplete = Url.Action("QuickSearch", "Employee") })
produces:
<input autocomplete="/App/Employee/QuickSearch" class="input-mini" id="lastName" name="lastName" type="text" value="" />
Notice that this URL does not contain the "PHILSAPP" part. This also happens if I use the URL helper in javascript or just about anywhere other than an "<a>" tag. Does anyone have any idea why this would be happening? As far as I can tell, both calls to Url.Action are pretty much the same so I cannot figure out why this is happening. I apologize if this question has already been answered but I was not able to find any information about someone having a similar problem. Thanks in advance for any assistance.
Update:
As requested, my routes are below:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Update 2: The access manager being used is Tivoli Identity Manager if that gives anyone any clues.
As nemesv pointed out above, the answer was that the Url.Action method was always generating the URL as /App/... but the access manager application would recognize certain tags (such as <a href="/App/...">, <form action="/App/...">, etc.) and would add the /PHILSAPP to the beginning. The solution I am exploring is to write some extension methods for the UrlHelper and HtmlHelper to generate absolute URLs rather than relative URLs where the host name including the /PHILSAPP will be specified in the web.config file. If anyone still has any other suggestions to solve this I would be happy to hear them but otherwise I am satisfied with using this as a work-around.
Some boilerplate code to start with:
namespace MvcApplicationNameSpace
{
/// <summary>
/// Extension methods to the UrlHelper class for generating absolute URLs using
/// Web.config settings
/// </summary>
public static class UrlHelperExtensions
{
private static string BaseUrl
{
get
{
return System.Configuration.ConfigurationManager.AppSettings["BaseUrl"];
}
}
/// <summary>
/// Generates a string for the absolute URL to an action method
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url)
{
return BaseUrl + url.Action();
}
/// <summary>
/// Generates a string for the absolute URL to an action method with the
/// specified name
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url, string actionName)
{
return BaseUrl + url.Action(actionName);
}
/// <summary>
/// Generates a string for the absolute URL to an action method with the
/// specified name and route values
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url, string actionName, object routeValues)
{
return BaseUrl + url.Action(actionName, routeValues);
}
/// <summary>
/// Generates a string for the absolute URL to an action method with the
/// specified name and route values
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url, string actionName, RouteValueDictionary routeValues)
{
return BaseUrl + url.Action(actionName, routeValues);
}
/// <summary>
/// Generates a string for the absolute URL to an action method and
/// controller
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="controllerName"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url, string actionName, string controllerName)
{
return BaseUrl + url.Action(actionName, controllerName);
}
/// <summary>
/// Generates a string for the absolute URL to an action method and
/// controller, including route values
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="controllerName"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url, string actionName, string controllerName, object routeValues)
{
return BaseUrl + url.Action(actionName, controllerName, routeValues);
}
/// <summary>
/// Generates a string for the absolute URL to an action method and
/// controller, including route values
/// </summary>
/// <param name="url"></param>
/// <param name="actionName"></param>
/// <param name="controllerName"></param>
/// <param name="routeValues"></param>
/// <returns></returns>
public static string AbsoluteAction(this UrlHelper url, string actionName, string controllerName, RouteValueDictionary routeValues)
{
return BaseUrl + url.Action(actionName, controllerName, routeValues);
}
}
}
We exactly had the same problem behind our secure entry server.
For REST calls, we wanted to generate urls on the server side to make them available in java script. But they didn't include the sub-path added by the secure entry server.
So we came up with a workaround like that (rendered in layout page):
<a id="urlBase" href="/" style="display: none;"></a>
<script type="text/javascript">
baseUrl = document.getElementById('urlBase').getAttribute('href');
</script>
href="/" has been substitued by the entry server by href="/path/", and we could easily concatenate baseUrl with our relative paths when accessing REST services.
Hope it helps in your case too.
Related
Masters,
We have implemented CacheManager and SessionManager in past applications by various ways, like by creating a SessionHelper static class and a CacheHelper static class.
Though it works fine, we lack some ability for generalization and globalization perspective.
So for new scratch development, we intend best practices for such general implementation in terms of flexibility and extensibility.
Please suggest.
You could create an interface to define the common operations used in Caching and Session management, named something like IStateManager. e.g.
/// <summary>
/// An interface to provide access to a state storage implementation
/// </summary>
public interface IStateManager
{
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
T Get<T>(string key);
/// <summary>
/// Adds the specified key and object to the state manager.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
void Set(string key, object data);
/// <summary>
/// Adds the specified key and object to the state manager.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
void Set(string key, object data, int cacheTime);
/// <summary>
/// Gets a value indicating whether the value associated with the specified key is in the state manager.
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
bool IsSet(string key);
/// <summary>
/// Removes the value with the specified key from the state manager.
/// </summary>
/// <param name="key">/key</param>
void Remove(string key);
/// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
void RemoveByPattern(string pattern);
/// <summary>
/// Clear all state manager data
/// </summary>
void Clear();
}
Then, you could create implementations of the interface to provide different functionality. E.g. an in memory implementation, that uses System.Runtime.Caching
/// <summary>
/// Represents an in memory cache
/// </summary>
public class MemoryCacheManager : IStateManager
{
public MemoryCacheManager()
{
}
protected ObjectCache Cache
{
get
{
return MemoryCache.Default;
}
}
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
public T Get<T>(string key)
{
return (T)Cache[key];
}
/// <summary>
/// Adds the specified key and object to the cache with a default cache time of 30 minutes.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
public void Set(string key, object data)
{
Set(key, data, 30);
}
/// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
public void Set(string key, object data, int cacheTime)
{
if (data == null)
return;
var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
}
/// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
public bool IsSet(string key)
{
return (Cache.Contains(key));
}
/// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
public void Remove(string key)
{
Cache.Remove(key);
}
/// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
public void RemoveByPattern(string pattern)
{
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>();
foreach (var item in Cache)
if (regex.IsMatch(item.Key))
keysToRemove.Add(item.Key);
foreach (string key in keysToRemove)
{
Remove(key);
}
}
/// <summary>
/// Clear all cache data
/// </summary>
public void Clear()
{
foreach (var item in Cache)
Remove(item.Key);
}
}
You could create multiple implementations of this interface, such as a 'Memcached' implementation to provide distributed caching for your application or a 'Session' implementation to provide user session based functionality.
Then, you can use your dependency container of choice to inject the implementations into your services\controllers and wire up your application.
Try and avoid static classes which can be problematic to unit test.
Can use the filter attributes for caching and can handle session through the singleton class..
http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
you can get some samples in the above link for the best way or approach to be followed.
I have an ASP.NET MVC 4 app where I have two areas:
Backstage
Signup
I have a class called DomainRoute that makes it possible to route an entire subdomain to an area:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Routing;
namespace Admin.Web.PresentationLogic
{
/// <summary>
/// DomainRoute is an extension of the default Route, that makes it possible to route domains and subdomains the specific controllers.
/// </summary>
public class DomainRoute : Route
{
private string _subDomain;
private string[] _namespaces;
/// <summary>
/// Initializes a new instance of the <see cref="DomainRoute"/> class.
/// </summary>
/// <param name="subDomain">The sub domain.</param>
/// <param name="url">The URL format.</param>
/// <param name="defaults">The defaults.</param>
public DomainRoute(string subDomain, string url, RouteValueDictionary defaults)
: base(url, defaults, new MvcRouteHandler())
{
this._subDomain = subDomain;
}
/// <summary>
/// Initializes a new instance of the <see cref="DomainRoute" /> class.
/// </summary>
/// <param name="subDomain">The sub domain.</param>
/// <param name="url">The URL format.</param>
/// <param name="defaults">The defaults.</param>
/// <param name="namespaces">The namespaces.</param>
public DomainRoute(string subDomain, string url, RouteValueDictionary defaults, string[] namespaces)
: base(url, defaults, new MvcRouteHandler())
{
this._subDomain = subDomain;
this._namespaces = namespaces;
}
/// <summary>
/// Returns information about the requested route.
/// </summary>
/// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
/// <returns>
/// An object that contains the values from the route definition.
/// </returns>
public override RouteData GetRouteData(System.Web.HttpContextBase httpContext)
{
// Request information
string requestDomain = httpContext.Request.Headers["HOST"];
if (!string.IsNullOrEmpty(requestDomain))
{
if (requestDomain.IndexOf(":") > 0)
{
requestDomain = requestDomain.Substring(0, requestDomain.IndexOf(":"));
}
}
else
{
requestDomain = httpContext.Request.Url.Host;
}
var index = requestDomain.IndexOf(".");
if (index < 0)
{
return RouteTable.Routes["Default"].GetRouteData(httpContext);
}
var subDomain = requestDomain.Substring(0, index);
if (!String.IsNullOrWhiteSpace(subDomain))
{
if (this._subDomain.Equals(subDomain, StringComparison.InvariantCultureIgnoreCase))
{
RouteData data = new RouteData(this, this.RouteHandler);
// Add defaults first
if (Defaults != null)
{
foreach (KeyValuePair<string, object> item in Defaults)
{
data.Values[item.Key] = item.Value;
}
}
var pathRegex = this.CreateRegex(Url);
var requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
// Match domain and route
Match pathMatch = pathRegex.Match(requestPath);
// Iterate matching path groups
for (int i = 1; i < pathMatch.Groups.Count; i++)
{
Group group = pathMatch.Groups[i];
if (group.Success)
{
string key = pathRegex.GroupNameFromNumber(i);
if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
{
if (!string.IsNullOrEmpty(group.Value))
{
data.Values[key] = group.Value;
}
}
}
}
if (!data.Values.ContainsKey("action"))
{
data.Values.Add("action", "Index");
}
data.DataTokens["Namespaces"] = this._namespaces;
data.DataTokens["area"] = data.Values["area"] ?? this._subDomain;
return data;
}
}
return RouteTable.Routes["Default"].GetRouteData(httpContext);
}
/// <summary>
/// Creates the regex.
/// </summary>
/// <param name="source">The source.</param>
/// <returns>Returns the Regex for the source.</returns>
private Regex CreateRegex(string source)
{
// Perform replacements
source = source.Replace("/", #"\/?");
source = source.Replace(".", #"\.?");
source = source.Replace("-", #"\-?");
source = source.Replace("{", #"(?<");
source = source.Replace("}", #">([a-zA-Z0-9_]*))");
return new Regex("^" + source + "$");
}
}
}
When I register the areas, I do this:
context.Routes.Add("Signup_default",
new DomainRoute("signup", "{controller}/{action}/{id}",
new RouteValueDictionary(new { area = "Signup", controller = "Home", action = "Index", id = UrlParameter.Optional }),
new string[] { "Admin.Web.Areas.Signup.Controllers" }));
So, the problem has to do with the way the DomainRoute's GetRouteData method is executed.
Whenever I try to access signup.localhost the instance of the DomainRoute class is the one that I used when registering the Backstage area.
I tried disabling the Backstage area, and then the Signup area worked.
It uses the instance of DomainRoute that occurs first in the RouteTable.
What am I missing?
I was missing the concept of the RouteCollection.
After step debugging into the .Net source, I realized that in my DomainRoute, if the sub-domain doesn't match, instead of returning default route data I should return null.
That's the way ASP.NET Routing determines which one to use -- by calling GetRouteData with the HttpContext and let the specific route figure out if "I'm a match for the HttpContext?"
Has anybody got any examples of using the Dynatree plugin with MVC? I have been wrestling with it without much progress. I have an action method which returns a JsonResult (but selects all columns in the underlying table, not sure if this is the problem) and in my initajax call , all I'm doing is calling this method.
If it's not too much trouble, I am looking for sample View and Controller action methods.
Thanks in advance for any help
You need to create an object to serialize the nodes eg.
public interface ITreeItem
{
}
/// <summary>
/// Tree Item Leaf.
/// </summary>
public class TreeItemLeaf :ITreeItem
{
/// <summary>
/// Gets the Title.
/// </summary>
public string title;
/// <summary>
/// Gets the Tooltip.
/// </summary>
public string tooltip;
/// <summary>
/// Gets the key.
/// </summary>
public string key;
/// <summary>
/// Gets the Data.
/// </summary>
public string addClass;
/// <summary>
/// Gets the Children.
/// </summary>
public IList<ITreeItem> children;
/// <summary>
/// Gets the rel attr.
/// </summary>
public string rel;
/// <summary>
/// Gets the State.
/// </summary>
public bool isFolder;
/// <summary>
/// Gets the State.
/// </summary>
public bool isLazy;
/// <summary>
/// Initializes a new instance of the <see cref="TreeItemLeaf"/> class.
/// </summary>
public TreeItemLeaf()
{
children = new List<ITreeItem>();
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeItemLeaf"/> class.
/// </summary>
/// <param name="type">The type of node.</param>
/// <param name="id">The Id of the node.</param>
/// <param name="title">The Title of the node.</param>
/// <param name="tooltip">The Tooltip of the node.</param>
public TreeItemLeaf(String type, Guid id, String title, String tooltip)
{
key = id.ToString();
this.title = title;
isFolder = false;
isLazy = false;
this.tooltip = tooltip;
children = new List<ITreeItem>();
}
}
/// <summary>
/// Tree Item.
/// </summary>
public class TreeItem : TreeItemLeaf
{
/// <summary>
/// Gets the State.
/// </summary>
public new bool isFolder;
/// <summary>
/// Initializes a new instance of the <see cref="TreeItem"/> class.
/// </summary>
public TreeItem() : base()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeItem"/> class.
/// </summary>
/// <param name="type">The type of node.</param>
/// <param name="id">The Id of the node.</param>
/// <param name="title">The Title of the node.</param>
/// <param name="tooltip">The tooltip of the node.</param>
public TreeItem(String type, Guid id, String title, String tooltip) : base(type, id, title, tooltip)
{
isFolder = true;
isLazy = true;
}
}
Once you have this, you can return a Json(IList<ITreeItem>) which you will need to build up from your results..
If you go to the Dynatee demo http://wwwendt.de/tech/dynatree/doc/samples.html , you can use Firefox/Firebug to study the HTTP requests to see exactly what is being passed in and returned.
My tree in the view is as follows :
// --- Initialize first Dynatree -------------------------------------------
$("#tree").dynatree({
fx: { height: "toggle", duration: 500 },
selectMode: 1,
clickFolderMode: 1,
children : #Html.Raw(String.Format("{0}", ViewData["tree"]).Replace("\"children\":[],", "")),
onLazyRead: function (node) {
node.appendAjax({
url: "#Url.Action("treedata", "tree")",
type: "GET",
data: { "id": node.data.key, // Optional url arguments
"mode": "all"
},
error: function(node, XMLHttpRequest, textStatus, errorThrown) {
}
}
});
}, //.... cut short for brevity
I am embeding the initial tree state in the "children:" part. And the Ajax reading is being set up in the "onLazyRead:" part.
My Ajax call is:
public JsonResult TreeData(FormCollection form)
{
return GetTreeData(Request.QueryString["id"], Request.QueryString["uitype"]);
}
The GetTreeData() function returns Json(ITreeItem);
I would recommend you use Firefox/Firebug and its "NET" function to see what is going and coming back.
Hope that helps.
I've just found Dynatree and I'm using it on my MVC project. Here's an example of how I did it. I decided to just put the data directly in the View like the basic example.
My data is a list of cities within California, grouped by county.
My controller simply passes a view model to my View and the view model has a CitiesAvailable property:
public IEnumerable<City> CitiesAvailable { get; set; }
My list of City objects is grabbed from the database (EF4) and the actual City object is the following:
In my View I create a ul containing the list of counties and their cities (I'm using Razor but webforms should be easy enough to figure out):
<div id="tree">
<ul id="treedata" style="display: none;">
#foreach (var county in Model.CitiesAvailable.Select(c => c.County).Distinct().OrderBy(c => c))
{
<li data="icon: 'false'">#county
<ul>
#foreach (var city in Model.CitiesAvailable.Where(c => c.County == county).OrderBy(c => c.Name))
{
<li data="icon: 'false'" id="#city.Id">#city.Name</li>
}
</ul>
</li>
}
</ul>
</div>
Then in my JavaScript I use the following:
$("#tree").dynatree({
checkbox: true,
selectMode: 3,
fx: { height: "toggle", duration: 200 }
});
It works great! Here's a sample of the output with a few items checked:
Let me know if anything doesn't make sense.
Note, I use data="icon: 'false'" in my li elements because I don't want the icons.
You can simply convert the object to json string, and send it to server as text
this is the js code:
var dict = $("#tree").dynatree("getTree").toDict();
var postData = JSON.stringify(dict["children"]);
$.ajax({ type: "POST",
url: "/UpdateServer/SaveUserTree",
data: {tree:postData},
dataType: "html"
});
And this is the controller code:
[HttpPost]
public void SaveUserTree(string tree = "")
{
HttpContext.Application["UserTree"] = tree;
}
You can send this string data back to client
if (HttpContext.Application["UserTree"] != null)
ViewBag.TreeData = new HtmlString(HttpContext.Application["UserTree"].ToString());
And finally, you can initial the tree, in the View with this data:
var treeData= #(ViewBag.TreeData)
$(function(){
// --- Initialize sample trees
$("#tree").dynatree({
children: treeData
});
});
Say I want to send an email from my MVC application containing a link back to an item.
So I want something in my controller like:
string link = "www.mysite/mycontroller/itemdetails/23";
can I retrieve this programatically so it works on whatever server/configuration i happen stick it on?
eg
string link = GetCurrentActionRoute() + "23";
public ActionResult Index()
{
string link = Url.Action(
(string)RouteData.Values["action"],
(string)RouteData.Values["controller"],
new { id = "23" },
Request.Url.Scheme
);
// TODO: do something with the generated link
return View();
}
I use the extension methods below from my Controller classes to generate links to various actions.
Examples:
string link = this.Action("Play");
string fullLink = this.FullUrlAction("Play", new { id = 1 });
Methods:
/// <summary>
/// Helper Extensions to the Controller class.
/// </summary>
public static class ControllerExtensions
{
/// <summary>
/// Get an action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="action">Action to provide the link for.</param>
/// <returns>Link Url for the Action.</returns>
public static string Action(this Controller controller, string action)
{
RouteValueDictionary rvd = new RouteValueDictionary
{
{ "controller", controller.GetType().Name },
{ "action", action }
};
return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
}
/// <summary>
/// Get an action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <returns>Link Url for the action with parameters.</returns>
public static string Action(this Controller controller, string action, object parameters)
{
string controllerName = controller.GetType().Name;
if (controllerName.EndsWith("Controller"))
{
controllerName = controllerName.Substring(0, controllerName.Length - 10);
}
return controller.Action(controllerName, action, parameters);
}
/// <summary>
/// Get an action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="targetController">Target controller for action.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <returns>Link Url for the action on the given controller.</returns>
public static string Action(this Controller controller, string targetController, string action, object parameters)
{
RouteValueDictionary rvd = new RouteValueDictionary(parameters)
{
{ "controller", targetController },
{ "action", action }
};
return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
}
/// <summary>
/// Get a fully qualified action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <param name="requestUrl">Current request URI.</param>
/// <returns>Fully qualified Link Url.</returns>
public static string FullUrlAction(this Controller controller, string action, object parameters, Uri requestUrl)
{
string controllerName = controller.GetType().Name;
if (controllerName.EndsWith("Controller"))
{
controllerName = controllerName.Substring(0, controllerName.Length - 10);
}
return controller.FullUrlAction(controllerName, action, parameters, requestUrl);
}
/// <summary>
/// Get a fully qualified action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="targetController">Target controller for action.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <param name="requestUrl">Current request URI.</param>
/// <returns>Fully Qualified Link Url.</returns>
public static string FullUrlAction(this Controller controller, string targetController, string action, object parameters, Uri requestUrl)
{
string testUrl = VirtualPathUtility.ToAbsolute(controller.Action(targetController, action, parameters));
return new Uri(requestUrl, testUrl).ToString();
}
}
If the link will point to the same location as that of the application ,you may consider using the Url.Action(...) and its overloads.
Eg.
string actionRoute = Url.Action("action method name","controller name");
If you want to get the complete path, you can use :
string domainRoute = HttpContext.Request.Url.AbsolutePath;
and then concatenate the id with that.
I know to use Html.ActionLink() to render textual <a href..."> links to actions.
How do I render a link to an action that has an underlying image as the link?
<img src="asdfasdf"/>
Here is code for the ImageLink HtmlHelper extension I use.
/*
* Image Link HTML helper
*/
/// <summary>
/// return image link
/// </summary>
/// <param name="helper"></param>
/// <param name="imageUrl">URL for image</param>
/// <param name="controller">target controller name</param>
/// <param name="action">target action name</param>
/// <param name="linkText">anchor text</param>
public static string ImageLink(this HtmlHelper helper, string imageUrl, string controller, string action, string linkText)
{
return ImageLink(helper, null, controller, action, linkText, imageUrl, null, null, null, null);
}
/// <summary>
/// return image link
/// </summary>
/// <param name="helper"></param>
/// <param name="imageUrl">URL for image</param>
/// <param name="controller">target controller name</param>
/// <param name="action">target action name</param>
/// <param name="linkText">anchor text</param>
/// <param name="htmlAttributes">anchor attributes</param>
public static string ImageLink(this HtmlHelper helper, string imageUrl, string controller, string action, string linkText, object htmlAttributes)
{
return ImageLink(helper, null, controller, action, linkText, imageUrl, null, null, new RouteValueDictionary(htmlAttributes), null);
}
/// <summary>
/// return image link
/// </summary>
/// <param name="helper"></param>
/// <param name="imageUrl">URL for image</param>
/// <param name="controller">target controller name</param>
/// <param name="action">target action name</param>
/// <param name="linkText">anchor text</param>
/// <param name="htmlAttributes">anchor attributes</param>
/// <param name="routeValues">route values</param>
public static string ImageLink(this HtmlHelper helper, string imageUrl, string controller, string action, string linkText, object htmlAttributes, object routeValues)
{
return ImageLink(helper, null, controller, action, linkText, imageUrl, null, null, new RouteValueDictionary(htmlAttributes), new RouteValueDictionary(routeValues));
}
/// <summary>
/// return image link
/// </summary>
/// <param name="helper"></param>
/// <param name="id">Id of link control</param>
/// <param name="controller">target controller name</param>
/// <param name="action">target action name</param>
/// <param name="strOthers">other URL parts like querystring, etc</param>
/// <param name="strImageURL">URL for image</param>
/// <param name="alternateText">Alternate Text for the image</param>
/// <param name="strStyle">style of the image like border properties, etc</param>
/// <returns></returns>
public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string linkText, string strImageURL, string alternateText, string strStyle)
{
return ImageLink(helper, id, controller, action, linkText, strImageURL, alternateText, strStyle, null, null);
}
/// <summary>
/// return image link
/// </summary>
/// <param name="helper"></param>
/// <param name="id">Id of link control</param>
/// <param name="controller">target controller name</param>
/// <param name="action">target action name</param>
/// <param name="linkText">anchor text</param>
/// <param name="strImageURL">URL for image</param>
/// <param name="alternateText">Alternate Text for the image</param>
/// <param name="strStyle">style of the image like border properties, etc</param>
/// <param name="htmlAttributes">html attribues for link</param>
/// <returns></returns>
public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string linkText, string strImageURL, string alternateText, string strStyle, IDictionary<string, object> htmlAttributes, RouteValueDictionary routeValues)
{
// Build the img tag
TagBuilder image = new TagBuilder("img");
image.MergeAttribute("src", strImageURL);
image.MergeAttribute("alt", alternateText);
image.MergeAttribute("valign", "middle");
image.MergeAttribute("border", "none");
TagBuilder span = new TagBuilder("span");
// Create tag builder
var anchor = new TagBuilder("a");
var url = new UrlHelper(helper.ViewContext.RequestContext).Action(action, controller, routeValues);
// Create valid id
anchor.GenerateId(id);
// Add attributes
//anchor.MergeAttribute("href", "/" + controller + "/" + action); //form target URL
anchor.MergeAttribute("href", url);
anchor.MergeAttribute("class", "actionImage");
if (htmlAttributes != null)
anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
// place the img tag inside the anchor tag.
if (String.IsNullOrEmpty(linkText))
{
anchor.InnerHtml = image.ToString(TagRenderMode.Normal);
}
else
{
span.InnerHtml = linkText;
anchor.InnerHtml = image.ToString(TagRenderMode.Normal) + " " + span.ToString(TagRenderMode.Normal);
}
// Render tag
return anchor.ToString(TagRenderMode.Normal); //to add </a> as end tag
}
Here is a post about creating strongly typed ActionImage extensions. Should get you started if you don't like those horrible error-prone magic strings.
<%=Html.ActionLink(
Html.Image("~/Images/bigwave.jpg"),
new {controller="Hurr", action="Durr"})) %>
Check here for how to create the Image method
Alternately, just write it in:
<%=Html.ActionLink(
"<img src=\"asdfasdf\"/>",
new {controller="Hurr", action="Durr"}) %>
Two options I can think of, I'll give you the suggested one first:
One: Give the anchor an a unique ID and use CSS to style the link appropriately. This will also give you the ability to easily apply a rollover image using :hover.
<%=Html.ActionLink(" ", "action", "controller", null, new { #class="sample" })%>
<style type="text/css">
a.sample { background-image: url(http://sstatic.net/so/img/replies-off.png); }
a.sample:hover { background-image: url(http://sstatic.net/so/img/logo.png); }
</style>
Two: Create your own HtmlHelper that either doesn't escape the linkText parameter like ActionLink does or takes an image URL.
If you are on ASP.Net MVC 1.0 you can get the futures library and do this:
<%= Html.SubmitImage("controlName", "~/ImagePath/ImageName.jpg") %>
You just need to add this library: Microsoft.Web.Mvc download the dll here
This is also supposed to be part of ASP.Net MVC 2.0 at some point.
I have tested the ImageLink helpers and suggest that they should be made to return MvcHtmlString instead of string to prevent the Razor engine from encoding the actual image URLs.
So I changed all the signatures of the ImageLink functions to return 'MvcHtmlString' instead of plain 'string' and changed the last line of the last version of 'ImageLink' to:
return MvcHtmlString.Create( anchor.ToString(TagRenderMode.Normal)); //to add </a> as end tag