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?"
Related
I've set up my menu using MVCSiteMap and I have this node:
<mvcSiteMapNode title="Courses Form" controller="Booking" action="Course" roles="CORLIC, VIEWCOBO"/>
I'm trying to enforce that this node must have roles "CORLIC" AND "VIEWCOBO" for it to be visible but of course this means that it will be displayed if the user has either of the above.
Is this possible?
Thanks.
The roles attribute is for interoperability with ASP.NET and should not be used in an MVC-only application.
For MVC, if you are already defining the AuthorizeAttribute on your controller actions, MvcSiteMapProvider will automatically pick them up and hide the matching nodes accordingly if security trimming is enabled.
[Authorize]
public ActionResult Course()
{
return View();
}
[Authorize]
[HttpPost]
public ActionResult Course(CourseModel model)
{
if (ModelState.IsValid)
{
// Implementation omitted
}
// If we got this far, something failed, redisplay form
return View(model);
}
The default AuthorizeAttribute accepts roles, but it works in the same way as the roles attribute - that is, any role that the user is in will cause it to succeed.
However, you could inherit AuthorizeAttribute yourself and override the IsAuthorized method to change the logic as needed.
public class SpecialAuthorizeAttribute : AuthorizeAttribute
{
private string _requiredRoles;
private string[] _requiredRolesSplit = new string[0];
/// <summary>
/// Gets or sets the required roles. The user must be a member of all roles for it to succeed.
/// </summary>
/// <value>
/// The roles string.
/// </value>
/// <remarks>Multiple role names can be specified using the comma character as a separator.</remarks>
public string RequiredRoles
{
get { return _requiredRoles ?? String.Empty; }
set
{
_requiredRoles = value;
_requiredRolesSplit = SplitString(value);
}
}
/// <summary>
/// Determines whether access for this particular request is authorized. This method uses the user <see cref="IPrincipal"/>
/// returned via <see cref="HttpRequestContext.Principal"/>. Authorization is denied if the user is not authenticated,
/// the user is not in the authorized group of <see cref="Users"/> (if defined), or if the user is not in any of the authorized
/// <see cref="Roles"/> (if defined).
/// </summary>
/// <param name="actionContext">The context.</param>
/// <returns><c>true</c> if access is authorized; otherwise <c>false</c>.</returns>
protected override bool IsAuthorized(HttpActionContext actionContext)
{
if (actionContext == null)
{
throw new ArgumentNullException("actionContext");
}
IPrincipal user = actionContext.ControllerContext.RequestContext.Principal;
if (user == null || user.Identity == null || !user.Identity.IsAuthenticated)
{
return false;
}
// Ensure all of the roles in RequiredRoles are present.
if (_requiredRolesSplit.Length > 0 && !_requiredRolesSplit.All(user.IsInRole))
{
return false;
}
// Call the base class to check the users and roles there.
return base.IsAuthorized(actionContext);
}
/// <summary>
/// Splits the string on commas and removes any leading/trailing whitespace from each result item.
/// </summary>
/// <param name="original">The input string.</param>
/// <returns>An array of strings parsed from the input <paramref name="original"/> string.</returns>
internal static string[] SplitString(string original)
{
if (String.IsNullOrEmpty(original))
{
return new string[0];
}
var split = from piece in original.Split(',')
let trimmed = piece.Trim()
where !String.IsNullOrEmpty(trimmed)
select trimmed;
return split.ToArray();
}
}
Then you can specify which roles are required by using the new property.
[SpecialAuthorize(RequiredRoles = "CORLIC, VIEWCOBO")]
public ActionResult Course()
{
return View();
}
[SpecialAuthorize(RequiredRoles = "CORLIC, VIEWCOBO")]
[HttpPost]
public ActionResult Course(CourseModel model)
{
if (ModelState.IsValid)
{
// Implementation omitted
}
// If we got this far, something failed, redisplay form
return View(model);
}
Another possible option is to use FluentSecurity as shown here. For FluentSecurity v2.0 to work with MvcSiteMapProvider, you need to copy the HandleSecurityAttribute code into your project and change it to inherit from AuthorizeAttribute instead of Attribute, then use it as specified in the FluentSecurity documentation.
I have a MVC5 application and set my hibernate stuff up like that:
public class PersistenceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
//Nhibernate session factory
Component.For<ISessionFactory>().UsingFactoryMethod(CreateNhSessionFactory).LifeStyle.Singleton,
Component.For<ISession>().UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession()).LifestylePerWebRequest(),
//All repoistories
Classes.FromAssembly(Assembly.GetAssembly(typeof(HdtRepository))).InSameNamespaceAs<HdtRepository>().WithService.DefaultInterfaces().LifestyleTransient()
);
}
and my base repository looks like that:
public abstract class RepositoryBase<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey> where TEntity : Entity<TPrimaryKey>
{
/// <summary>
/// Gets the NHibernate session object to perform database operations.
/// </summary>
public ISession Session { get; set; }
/// <summary>
/// Used to get a IQueryable that is used to retrive object from entire table.
/// </summary>
/// <returns>IQueryable to be used to select entities from database</returns>
public IQueryable<TEntity> GetAll()
{
return Session.Query<TEntity>();
}
/// <summary>
/// Gets an entity.
/// </summary>
/// <param name="key">Primary key of the entity to get</param>
/// <returns>Entity</returns>
public TEntity Get(TPrimaryKey key)
{
return Session.Get<TEntity>(key);
}
/// <summary>
/// Inserts a new entity.
/// </summary>
/// <param name="entity">Entity</param>
public void Insert(TEntity entity)
{
Session.Save(entity);
}
/// <summary>
/// Updates an existing entity.
/// </summary>
/// <param name="entity">Entity</param>
public void Update(TEntity entity)
{
Session.Update(entity);
}
/// <summary>
/// Deletes an entity.
/// </summary>
/// <param name="id">Id of the entity</param>
public void Delete(TPrimaryKey id)
{
Session.Delete(Session.Load<TEntity>(id));
}
}
Everything works corret when I Insert a entity.
Update is onyl working when I add a Session.Flush() to my Update method.
This is the mehtod which is called from Ajax and performs insert or update.
[Authorize]
[ValidateInput(false)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public JsonNetResult CreateOrUpdateTimeRecord(TimeRecord tr)
{
TimeRecord trLocal;
if (tr.Id == -1 || tr.Id == 0)
{
trLocal = new TimeRecord();
trLocal.Description = tr.Description;
trLocal.StartTime = tr.StartTime;
trLocal.EndTime = tr.EndTime;
trLocal.User = _userRepo.Get(tr.User.Id);
trLocal.Hdt = _hdtRepo.Get(tr.Hdt.Id);
_timeRepo.Insert(trLocal);
}
else
{
trLocal = _timeRepo.Get(tr.Id);
trLocal.Description = tr.Description;
trLocal.StartTime = tr.StartTime;
trLocal.EndTime = tr.EndTime;
_timeRepo.Update(trLocal);
}
return new JsonNetResult() { Data = trLocal};
}
I dont understand why this works for insert and not for Update.
Do I have to care about transaction and opening/closing Sessions?
I thought that "LifestylePerWebRequest" would do that for me.
Cheers,
Stefan
:edit: (my initial answer was partially wrong)
You implementation should actually work fine (just tested it).
Though I can reproduce the behavior that updates are not flushed although the session object gets disposed correctly
The LifestylePerWebRequest actually takes care of that just fine, it will dispose the session whenever the request ends. Therefore you had to add the request handle to the web.config etc... So the windsor stuff is perfectly aware of the fact that ISession is disposable and that it has to dispose it...
This is because of the the FlushMode of the session.
The default mode is Auto, which might not be really what you want because you cannot rely on the changes getting stored (especially with update calls).
You can either change the FlushMode of the created session object, or, and this is what I recommend, use transactions.
Lets look at the following example:
var session = container.Resolve<ISession>();
var obj = new Paper()
{
Author = "Author",
Description = "Description",
};
session.Save(obj);
obj.Author = "Author2";
session.Update(obj);
In this case, the update will never be flushed/stored in the database. This is the behavior you currently have I guess.
Now lets add a transaction around it:
var session = container.Resolve<ISession>();
using (var transaction = session.BeginTransaction())
{
var obj = new Paper()
{
Author = "Author",
Description = "Description",
};
session.Save(obj);
obj.Author = "Author2";
session.Update(obj);
transaction.Commit();
}
Now the changes will be saved for sure.
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
});
});
I'm trying to make a wizard in MVC3 using Entity Framework. It needs to keep the state of an object (an article in this case) across a couple of steps.
I have a static variable in my controller that instantiates a new Article. In the different Actions I use TryUpdateModel to map the form to the static variable. The problem is, it seems that TryUpdateModel() updates the database as well. I need TryUpdateModel to do the automatic mapping, and update the static _article variable, but I don't want it to persist to the database until the last step!
N.B: I know there are a lot of possible solutions for creating a wizard in MVC, but I'd like to know what to do to make this way work, so please no alternatives for an MVC wizard-pattern.
Thanks.
namespace website.Controllers
{
public class ArticlesController : BaseController
{
// private static variable to hold the chosen article in the wizard
private static articles _article = new articles();
/// <summary>
/// Index page shows a list of articles in a webgrid
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
List<articles> _articles = Data.getArticles();
return View(_articles);
}
/// <summary>
/// First page of the article wizard
/// </summary>
/// <returns></returns>
public ActionResult BasicDetails(string id, string nextButton)
{
// back or next doesn't matter - store form values
if (_article != null) TryUpdateModel(_article);
if (nextButton != null)
{
return RedirectToAction("ArticleGroup");
}
else
{
_article = Data.GetArticleById(id);
return View(_article);
}
}
/// <summary>
/// Second page of the article wizard
/// </summary>
/// <returns></returns>
public ActionResult ArticleGroup(string nextButton, string backButton)
{
TryUpdateModel(_article);
if (backButton != null)
return RedirectToAction("BasicDetails");
else if (nextButton != null)
{
return RedirectToAction("Price");
}
else
{
return View(_article);
}
}
/// <summary>
/// Third page of the article wizard
/// </summary>
/// <returns></returns>
public ActionResult Price(string nextButton, string backButton)
{
TryUpdateModel(_article);
if (backButton != null)
{
return RedirectToAction("ArticleGroup");
}
else if (nextButton != null)
return RedirectToAction("LinkedClubs");
else
{
return View(_article);
}
}
/// <summary>
/// Last page of the article wizard
/// </summary>
/// <returns></returns>
public ActionResult LinkedClubs(string backButton)
{
if (backButton != null)
return RedirectToAction("Price");
else
return View(_article);
}
}
}
Rather than using a static variable to hold your state information (that is a critical error btw) you should pass a state bag holding the information that you need in between pages.
Usually data entities (entities mapped to database) and viewmodel entities (entities with that user works) used separately. When user posted data after some step - you make TryUpdateModel() to session object (specific for user, not for all application as static variable).
At last step you call business logic method UpdateModel(viewmodel), that update all columns by id of viewmodel, using all filled properties.
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.