Thursday, September 13, 2012

Working with File and Folder Paths in Code in MVC


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Files</title>
</head>
<body>
    <div>
        <h1>The ~ operator: Getting the virtual root</h1>
        @{
            var myImagesFolder = "../../Content/images";
            var myStyleSheet = "../../Content/Site.css";
            }

            <h1>The Server.MapPath method: Converting virtual to physical paths</h1>
            @{var dataFilePath = "~/dataFile.txt";}
            <!-- Displays a physical path C:\Websites\MyWebSite\datafile.txt -->
            <p>@Server.MapPath(dataFilePath)</p>


            <h1>The Href method: Creating paths to site resources</h1>
            <!--This code creates the path "../images/Logo.jpg" in the src attribute. -->
           
            <img src="@Href(myImagesFolder)/Chrysanthemum.jpg" alt="Image" width="100px" height="100px" />
            <br /><br />
            <!-- This produces the same result, using a path with ~ -->
            <img src="@Href("../../Content/images")/Chrysanthemum.jpg" alt="Image" width="100px" height="100px" />
            <!-- This creates a link to the CSS file. -->
            <link rel="stylesheet" type="text/css" href="@Href(myStyleSheet)" />

    </div>
</body>
</html>

Output:

D:\MyProjects\Registrationmvc3PArt2\CabAutomationSystem\CabAutomationSystem\dataFile.txt


No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...