初学者到MVC 4:需要从外部或其他类调用方法

时间:2021-07-12 05:54:39

I am very new to MVC 4. I am having a scenario where i need to define a set of methods in seperate class and call all those methods in an controller. so that i could handle functionality different from view.

我是MVC 4的新手。我有一个场景,我需要在单独的类中定义一组方法并在控制器中调用所有这些方法。这样我就可以处理与视图不同的功能。

Below are samples of my code.... Please review.

以下是我的代码示例....请查看。

i have added an new class file under controller and defined the methods as below:

我在控制器下添加了一个新的类文件,并定义了如下方法:

 public void Convert(ConvertFileOutputType outputType, FileInfo inFile, DirectoryInfo        outFolder, int timeout,
        int topMargin, int bottomMargin, int leftMargin, int rightMargin)
    {
       switch (outputType)
        {
            case ConvertFileOutputType.Pdf:
                break;
            default: break;
            // throw new    ConvertFileException(Properties.Resources.OutputTypeNotSupportedWordPerfect);
        }
        if (converter == null)
        {
            //converter.LicenseKey = "";
            converter = new PdfConverter();
            converter.LicenseKey = "";
            converter.PdfDocumentOptions.LiveUrlsEnabled = false;
            converter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Letter;
            converter.PdfDocumentOptions.TopMargin = topMargin;
            converter.PdfDocumentOptions.BottomMargin = bottomMargin;
            converter.PdfDocumentOptions.LeftMargin = leftMargin;
            converter.PdfDocumentOptions.RightMargin = rightMargin;
            converter.AvoidImageBreak = true;
        }
        converter.PdfDocumentInfo.AuthorName = "FileConvert";
        converter.PdfDocumentInfo.Title = inFile.Name;
        converter.PdfDocumentInfo.CreatedDate = DateTime.Now;
        converter.SavePdfFromHtmlFileToFile(inFile.FullName, outFolder + "\\" + inFile.Name + ".pdf");
    }

And i want to call this above method in an controller method.... i have blindly written code as....

我想在控制器方法中调用上面的方法....我已经盲目地编写了代码....

  public string UploadAsPDF(HttpPostedFileBase fileData)
     {
         var fileName = this.Server.MapPath("~/uploads/" +     System.IO.Path.GetFileName(fileData.FileName));
         fileData.SaveAs(fileName);


        Convert(outputType, inFile,outFolder,timeout, topMargin, bottomMargin, leftMargin, rightMargin);

         return "OK"; 
     }

whenever i tried to build its a type is being used as method Please advice....

每当我试图建立它的类型被用作方法请建议....

1 个解决方案

#1


0  

if the Convert method is in a class called Converters, you would need to instantiate the class, and then call Convert....

如果Convert方法在一个名为Converters的类中,则需要实例化该类,然后调用Convert ....

public Converters {
   public void Convert(...);
} 

You will then use like this...

然后你会像这样使用......

public string UploadAsPDF(HttpPostedFileBase fileData)
     {
         var fileName = this.Server.MapPath("~/uploads/" + System.IO.Path.GetFileName(fileData.FileName));
         fileData.SaveAs(fileName);


        var converters = new Converters();
        converters.Convert(outputType, inFile,outFolder,timeout, 
                           topMargin, bottomMargin, leftMargin, rightMargin);

         return "OK"; 
     }

#1


0  

if the Convert method is in a class called Converters, you would need to instantiate the class, and then call Convert....

如果Convert方法在一个名为Converters的类中,则需要实例化该类,然后调用Convert ....

public Converters {
   public void Convert(...);
} 

You will then use like this...

然后你会像这样使用......

public string UploadAsPDF(HttpPostedFileBase fileData)
     {
         var fileName = this.Server.MapPath("~/uploads/" + System.IO.Path.GetFileName(fileData.FileName));
         fileData.SaveAs(fileName);


        var converters = new Converters();
        converters.Convert(outputType, inFile,outFolder,timeout, 
                           topMargin, bottomMargin, leftMargin, rightMargin);

         return "OK"; 
     }