How to Get virtual path in asp.net MVC

Below are some ways which you can use to get virtual path of a controller.

If you are in controller and want to get virtual path of any controller, you can do it as follows :-

        public ActionResult RouteTest()
        {
            RouteValueDictionary routeValue = new RouteValueDictionary
            {
                { "controller","home"},{"action","about"}
            };
            ViewBag.controllerUrl = Url.RouteUrl(routeValue);
            return View();
        }

Here I am creating the routeValue object  which is of type of RouteValueDictonary and passing route information ,at first I am specifying controller name as “home” and then action value as “about” , then passing this object to Url.RouteUrl method which returns the virtual path of “about” action which are within the “HomeController” MVC controller.

You can get this “controllerUrl” value in the view as below,

@{
    ViewBag.Title = "RouteTest";
}

RouteTest

 
Virtual Path using Controller: @ViewBag.controllerUrl

 

For Views : In the same way you can get the virtual url on the MVC views below code do the same for you within the view,

@{
    var viewContext = this.ViewContext.RequestContext;
    var routeValue = new RouteValueDictionary
    {
       { "controller","home"},{"action","about"}
    };
    var routePath = RouteTable.Routes.GetVirtualPath(viewContext, routeValue);
    
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

We are creating the same RouteValueDictonary  object here as well , the only different is we are using RouteTable.Route.GetVirtualPath method which takes the current RequestContext object and RouteValueDictonary object to return an object which have virtual path information. You can retrieve it using below line of code in the view,

 

@{
    ViewBag.Title = "RouteTest";
}
 

RouteTest

@{
    var viewContext = this.ViewContext.RequestContext;
    var routeValue = new RouteValueDictionary
    {
       { "controller","home"},{"action","about"}
    };
    var routePath = RouteTable.Routes.GetVirtualPath(viewContext, routeValue);
    
}
 
 Virtual Path using viewContext : @routePath.VirtualPath

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

Thanks for reading, I hope it helped , Happy Coding. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s