If I have a url like "/something/something/" and my site is http://mysite.com and I want to link to that something url, is there a method like Url.Content(); which will discover the virtual directory of the site in IIS and map appropriately to a url path?
I tried Url.GenerateContentUrl(), Url.Action(), Url.Content(), Url.RouteUrl()
is there a method like Url.Content();
Why like when there is Url.Content?
var url = Url.Content("~/something/something");
which will take care of the virtual directory name and if your side is deployed at the root http://example.com it will return /something/something and if you have a virtual directory http://example.com/applicationname it will return /applicationname/something/something.
So everytime you need to link a static resource you should always use Url.Content. For example:
<img src="<%= Url.Content("~/images/foo.png") %>" alt="" />
will always correctly resolve the image url no matter where your site is deployed to.
If, for whatever reason, you can't just use Url.Content, you can use either HostingEnvironment.ApplicationVirtualPath or HttpContext.Current.Server.MapPath instead.
But I can't think of a good reason you can't just use Url.Content.
This is what I assume:
You have a file like myfile.jpg on the root on your site. Then you want a url like:
http://mysite.com/myfile.jpg
Right?
All you need to do is add this in yours routes in Global.asax.cs:
routes.IgnoreRoute("myfile.jpg");
Yes it should work on sub folders too.
Related
For example, I need only documents from a folder on the server drive X:\Docs for an online web application. Is there a way that a button on the website will open X:\Docs by default? I have tried this to open specific folders with no luck:
[HttpPost]
public ActionResult Index(HttpFileCollection file)
{
var path = System.IO.Path.GetDirectoryName("X:\Docs");
return RedirectToAction("Index");
}
I am new to C# and MVC. Is this achievable?
You can enable directory browsing of that folder and then having the button (or href) to point to the url. You don't event need a controller method for it.
Updated: if the folder is not under your website's root you will need to do some work by yourself. For example
#foreach (string path in Directory.GetFiles("X:\Docs"))
{
<div>
<!--doc link-->
</div>
}
You will need to have read permission for that drive ofc
As Luke pointed out you could alo do this inside your controller and pass it into your View which I also think it might be a better approach since View should be responsible for reading and rendering data
I am trying to shorten url of a site that my company develop using sitecore. I have been looking into Alex Shyba's blog post here and Sitecore documentation here, but it seems doesn't work. What I want to shorten is from localhost:8081/sitecore/Content/Sites/HeinzABCID/Dapur.aspx to localhost:8081/Dapur.aspx
That url is generated by LinkManager.GetItemUrl() method. My code is like below to get the option and the link.
UrlOptions opt = (UrlOptions)UrlOptions.DefaultOptions.Clone();
opt.SiteResolving = Sitecore.Configuration.Settings.Rendering.SiteResolving;
linkToResep.NavigateUrl = LinkManager.GetItemUrl(citem, opt);
While in my web.config I have put the configuration like below.
<linkManager defaultProvider="sitecore">
<providers>
<clear/>
<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel"
addAspxExtension="true"
alwaysIncludeServerUrl="false"
encodeNames="true"
languageEmbedding="asNeeded"
languageLocation="filePath"
shortenUrls="true"
useDisplayName="true"/>
</providers>
</linkManager>
The configuration/sitecore/sites/site with name="website" part in my web.config is below.
<site name="website" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/Content/Sites/HeinzABCID" startItem="/Home" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="10MB" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="5MB" filteredItemsCacheSize="2MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" language="en" disableClientData="false"/>
I don't have any other site, just that with default(shell, login, admin, service, modules_shell, and modules_website).
And here is the site structure of my site.
/sitecore/content/sites
+Sites
+---+HeinzABCID
+---+Dapur
+Search
+Other Items
Please help me :)
I had a similar problem, with /sitecore/content/site showing up in my URLs.
In my case it was site context that was wrong - running the sitecore shell context instead of the website context. Here's the code used to switch:
// Store the current site context name
string oldSiteName = Sitecore.Context.GetSiteName();
// Change the website context
Sitecore.Context.SetActiveSite("website");
// Generate the link
string url = LinkManager.GetItemUrl(item);
// Change back to the old site context
Sitecore.Context.SetActiveSite(oldSiteName);
It looks like your site configuration is incorrect. Based on your Sitecore tree I think it should be:
<site name="website" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/Content/Sites" startItem="/HeinzABCID" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize="10MB" registryCacheSize="0" viewStateCacheSize="0" xslCacheSize="5MB" filteredItemsCacheSize="2MB" enablePreview="true" enableWebEdit="true" enableDebugger="true" language="en" disableClientData="false"/>
You may also want to look at this article on Sitecore SDN for configuring multiple sites in a single Sitecore instance:
Sitecore SDN: Configuring Multiple Sites
According to this documentation, the URL will be created relative to the 'startItem' as defined in your 'website' site tag.
Using regex replace I successfully shorten the URL. I can also manage to use it on multiple site.
public static string ShortenURL(string URLToShorten)
{
return Regex.Replace(URLToShorten,#"sitecore/content/sites/[\w]{1,}/","");
}
The complete description of the solution that includes SEO can be found here.
It may also help if your site definition has a targethostname defined.
I'm using MVC.I want to save xml file inside the services class.
so wrote this one.
string path = HttpContext.Current.Server.MapPath((Url.Content("~/client-authentication.xml")));
but there is error and it says
'System.Security.Policy.Url' does not contain a definition for 'Content'
How to solve it??
How can I give the path??
Is this code in your view(.cshtml) or controller(.cs)?
if cshtml, you can write "string path = Url.Content(...)" directly, no need Server.MapPath.
if controller, just Server.MapPath(...), no need Url.Content.
PS, you can also use AppDomain.CurrentDomain.BaseDirectory to retrive physical path of your site root.
This is the wrong Url class. You want the System.MVC.Web.UrlHelper instance called Url which is a property provided on MVC controllers.
how to create a asp.net mvc form helper that takes virtual directory into consideration?
In testing our dev server has:
http://devserver1/some_virt_directory/
production is:
http://www.example.com
I need the form post url to reflect if we have a virtual directory or not, is this possible?
The existing form helper methods already take into account this:
<% using (Html.BeginForm("actionName", "controllerName")) { %>
...
<% } %>
You can use the UrlHelper.Content(..) method to resolve URLs taking into account what the real root is. The method will resolve the tilde ~ character. Look here for a related question on SO.
I have an existing site that I'd like to convert to use routing, and after reading Scott Guthrie's post here, I built a working sample that works for most circumstances. However, since not all of the pages on the existing site match a particular pattern, I'll need to check against a database to determine which route (destination .aspx page) to use.
For example, most pages are like this:
http://www.mysite.com/people/person.html
This is fine - I can easily route these to the view_person.aspx page because of the 'people' directory.
But some pages are like this:
http://www.mysite.com/category_page.html
http://www.mysite.com/product_page.html
This necessitates checking the database to see whether to route to the view_category.aspx page or the view_product.aspx page. And this is where I'm stuck. Do I create an IRouteHandler that checks the database and returns the route? Or is there a better way? The only code I've found that kind of fits is the answer to this question.
Thanks in advance.
If you don't mind doing so, the cleanest solution is to:
http://www.mysite.com/pages/category_page.html
In ASP.NET MVC, this situation would be handled a little differently, by specifying a default controller and action method on the root route.
Your route handler doesn't check the database. It sends all the requests to a handler .aspx script. It's that script that checks the database.
My register route looks like...
private static void RegisterRoutes()
{
Route currRoute = new Route("{resource}.axd/{*pathInfo}",
new StopRoutingHandler());
RouteTable.Routes.Add( "IgnoreHandlers", currRoute);
currRoute = new Route("{urlname}",
new EPCRouteHandler("~/Default.aspx"));
currRoute.Defaults = new RouteValueDictionary {{"urlname", "index.html"}};
RouteTable.Routes.Add( "Default", currRoute);
}
The custom handler, which shouldn't be needed in ASP.Net 4.0, simply passes the urlname parameter to the responding script as a URL variable.
Now how often the responding script checks the database depends on how often the data in the database is changed. You can easily cache paths and invalidate the cache when the data is suppose to have changed for instance.
For anyone stuck in the same situation, I ended up adapting the code from this answer to check against a database and return the proper ASPX page.